问题描述
创建例如assets/js/dependencies/app.io.js
与:
io.socket.on('connect', function socketConnected() {console.debug("这是来自连接:", io.socket);console.debug("WebSocket 已连接:", io.socket.isConnected());io.socket.get('/user', function(resData) {控制台调试(resData);});});
控制台
|>现在连接到 Sails.\___/如需帮助,请参阅:....(使用sails.io.js 浏览器SDK @v0.13.7)app.io.js:3 这是来自连接:SailsSocket {headers: undefined, eventQueue: Object, isConnecting: false, extraHeaders: Object, hostname: "localhost"...}app.io.js:4 WebSocket 已连接:trueapp.io.js:7 尚未在核心中实现 <========== 为什么?
为什么我会收到这条消息?
关于如何解决这个问题的任何指示?
我们需要更多信息例如:
- 你在使用 Angular 之类的 js 框架吗?
- 您是否使用 Bower 等工具管理依赖项?
- 您的 Sails 服务器是如何配置的?
最重要的是,我可以向您展示我如何配置我的 Sails 后端:
在我的 .sailsrc
中,我的钩子配置如下
钩子":{CSRF":假,咕噜":假,i18n":假,发布订阅":假,会话":假,插座":真的,意见":假}
然后在我的 UserController.js
中,我有一个启用套接字通信的简单方法
enableNtofications: function(req, res){//检查请求是否来自//一个套接字请求如果(req.isSocket){//获取当前登录的用户var user = req.user;//订阅客户端以进行模型更改User.subscribe(req, [user.id]);返回 res.ok();} 别的 {返回 res.badRequest();}},
我的前端使用 Angular 和一个 ngSails 模块,它是一种 'sails 的包装器.io' 用于 Angular
在我的UserService.js"中,我可以做类似的事情
//等待用户通知$sails.on('user', function(event) {如果(事件){//在这里管理消息...}});
然后调用服务器方法以启用套接字
//调用服务return $sails.post('/user/enableNtofications').then(//好的功能() {},//对功能(数据){console.log("启用通知KO");控制台日志(数据错误);});
(您还需要注入 '$sails' 模块并正确配置...)
希望能帮到你
Create e.g. 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);
});
});
Console
|> 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?
Why am I getting this message?
Any pointers on how to fix this?
we need more informations such as:
- are you using js frameworks like Angular?
- are you managing your dependencies with tools like Bower?
- how is your Sails server configured?
Above all i can show you how i configured my Sails backend:
in my .sailsrc
i have the hooks configured as follows
"hooks": {
"csrf": false,
"grunt": false,
"i18n": false,
"pubsub": false,
"session": false,
"sockets": true,
"views": false}
then in my UserController.js
i have this simple method that enables the socket communication
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();
}
},
my frontend uses Angular and a the ngSails module that is a sort of wrapper of 'sails.io' for Angular
and in my 'UserService.js' i can do something like
// waiting for notifications on User
$sails.on('user', function(event) {
if (event) {
// manage the message here...
}
});
and then call the server method in order to enable the sockets
// calling the service
return $sails.post('/user/enableNtofications').then(
// ok
function() {},
// ko
function(data) {
console.log("enable notifications KO");
console.log(data.error);
});
(you need also to inject the '$sails' module and configure it properly...)
Hope this could help you
这篇关于Sails.io.js io.socket.get('/user',...) 尚未在核心中实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!