Sails.js WebSocket 如何创建新通道?
Posted
技术标签:
【中文标题】Sails.js WebSocket 如何创建新通道?【英文标题】:Sails.js WebScoket how to create new channel? 【发布时间】:2016-05-25 17:55:37 【问题描述】:如何在 SAILS.js 中创建新的发射通道?
简单
io.sockets.in('ee').emit('messageName', thisIs: 'theMessage');
然后在 Angular.js 中
io.socket.on("ee", function(data)
console.log(data);
)
不工作..
【问题讨论】:
这引出了一个明显的问题..您是否在广播消息之前设置了事件处理程序“io.socket.on”? 不...怎么办? 【参考方案1】:您可以使用此代码,我认为它可以完成您想要的工作,但不是您提到的方式:
在客户端调用我们在服务器端使用的路由之前添加这些代码:
// Use `io.socket.on()` to listen for the 'hello' event:
io.socket.on('hello', function (data)
console.log('Socket `' + data.id + '` joined the party!');
);
在您想要的路由控制器中添加以下代码:
// Have the socket which made the request join the "funSockets" room.
sails.sockets.join(req, 'funSockets');
// Broadcast a notification to all the sockets who have joined
// the "funSockets" room, excluding our newly added socket:
sails.sockets.broadcast('funSockets', 'hello', howdy: 'hi there!', req);
注意这个“你好!”刚刚加入的客户端将不会收到消息。如果您想向所有客户端以及新加入的客户端发送消息,只需从广播参数中删除 req 参数即可。
欲了解更多信息,请查看此链接: http://sailsjs.com/documentation/concepts/realtime
【讨论】:
以上是关于Sails.js WebSocket 如何创建新通道?的主要内容,如果未能解决你的问题,请参考以下文章
Sails.js + Passport.js 通过 websockets 进行身份验证
Sails js:如何在 Sails js 的单个模型中定义两个 MySQL 连接到两个不同的数据库?