创建例如assets/js/dependencies/app.io.js
with:
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?
为什么我收到此消息?
有关如何解决此问题的任何指示?
答案 0 :(得分:2)
我们需要更多信息,例如:
最重要的是,我可以告诉你我如何配置我的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();
}
},
我的前端使用Angular和ngSails模块,它是&saff.io&#39;的一种包装。对于Angular
并在我的&#39; UserService.js&#39;我可以做点什么
// 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);
});
(您还需要注入&#39; $ sails&#39;模块并正确配置...)
希望这可以帮到你