使用创建例如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套接字获取文档
为什么我收到这个消息?
有关于如何解决这个问题的建议吗?
我们需要更多信息,例如:
- 你在用Angular这样的js框架吗
- 您是否使用Bower这样的工具来管理您的依赖关系
- 您的Sails服务器是如何配置的
最重要的是,我可以向你展示我如何配置我的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模块,后者是Angular 的"sails.io"的包装器
在我的"UserService.js"中,我可以做一些类似的事情
// waiting for notifications on User
$sails.on('user', function(event) {
if (event) {
// manage the message here...
}
});
然后调用服务器方法以启用套接字
// calling the service
return $sails.post('/user/enableNtofications').then(
// ok
function() {},
// ko
function(data) {
console.log("enable notifications KO");
console.log(data.error);
});
(您还需要注入"$sails"模块并对其进行正确配置…(
希望这能帮助你