简体   繁体   中英

Sails.js WebScoket how to create new channel?

How to create new channel for emit in SAILS.js?

simple

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

and then in 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.

For further information please check this link: http://sailsjs.com/documentation/concepts/realtime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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