创建例如:assets/js/dependencies/app.io.js:
io.socket.on('connect', function socketConnected() {
console.debug("This is from the connect: ", io.socket);
console.debug("WebSocket is connected:", io.socket.isConnected());
io.socket.get('/user', function(resData) {
console.debug(resData);
});
});控制台
|> Now connected to Sails.
\___/ For help, see: ....
(using sails.io.js browser SDK @v0.13.7)
app.io.js:3 This is from the connect: SailsSocket {headers: undefined, eventQueue: Object, isConnecting: false, extraHeaders: Object, hostname: "localhost"…}
app.io.js:4 WebSocket is connected: true
app.io.js:7 Not implemented in core yet <========= WHY?注:io-socket-get文档
我为什么要收到这条消息?
关于如何解决这个问题有什么建议吗?
发布于 2016-10-13 17:20:07
我们需要更多的信息,例如:
最重要的是,我可以向您展示如何配置我的Sails后端:
在我的.sailsrc中,我的钩子配置如下
"hooks": {
"csrf": false,
"grunt": false,
"i18n": false,
"pubsub": false,
"session": false,
"sockets": true,
"views": false}然后,在我的UserController.js中,我有一个简单的方法来启用套接字通信
enableNtofications: function(req, res){
// checking if the request comes from
// a socket request
if(req.isSocket){
// getting the current logged user
var user = req.user;
// subuscribing the client to model changes
User.subscribe(req, [user.id]);
return res.ok();
} else {
return res.badRequest();
}
},我的前端使用的是角,ngSails模块是角的“sails.io”包装器。
在我的“UserService.js”中,我可以做这样的事情
// waiting for notifications on User
$sails.on('user', function(event) {
if (event) {
// manage the message here...
}
});然后调用server方法,以便启用套接字。
// calling the service
return $sails.post('/user/enableNtofications').then(
// ok
function() {},
// ko
function(data) {
console.log("enable notifications KO");
console.log(data.error);
});(您还需要注入“$sails”模块并正确配置它.)
希望这能帮到你
https://stackoverflow.com/questions/40021125
复制相似问题