0

如何在 SAILS.js 中为发射创建新通道?

简单的

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

然后在 Angular.js

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

不工作。。

4

1 回答 1

0

您可以使用此代码,我认为它可以完成您想要的工作,但不是您提到的方式:

  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!');
    });
    
  2. 在您想要的路由控制器中添加以下代码:

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

于 2016-12-13T10:55:39.957 回答