简体   繁体   English

Sails.js WebScoket如何创建新频道?

[英]Sails.js WebScoket how to create new channel?

How to create new channel for emit in SAILS.js? 如何在SAILS.js中创建用于发出的新通道?

simple 简单

io.sockets.in('ee').emit('messageName', {thisIs: 'theMessage'});

and then in Angular.js 然后在Angular.js中

io.socket.on("ee", function(data){
  console.log(data);
})

don`t work.. 不要工作..

You can use this code, I think it does the work you want but not in the way you mentioned: 您可以使用此代码,我认为它可以完成您想要的工作,但不能达到您提到的方式:

  1. In client side before calling the route which we use in server side add these codes: 在客户端调用我们在服务器端使用的路由之前,请添加以下代码:

     // Use `io.socket.on()` to listen for the 'hello' event: io.socket.on('hello', function (data) { console.log('Socket `' + data.id + '` joined the party!'); }); 
  2. In your desired route controller add these codes: 在所需的路由控制器中,添加以下代码:

     // 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); 

    Note that this "hi there!" 请注意,此“嗨!” message will not be received by the client which just joined. 刚刚加入的客户端将不会收到该消息。 If you want to send a message to all the clients as well as the new joined one, just remove req parameter from broadcast arguments. 如果要向所有客户端以及新加入的客户端发送消息,只需从广播参数中删除req参数。

For further information please check this link: http://sailsjs.com/documentation/concepts/realtime 有关更多信息,请检查此链接: http : //sailsjs.com/documentation/concepts/realtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM