是否可以向所有活动的WebSocket连接发送消息?使用node.js或python tornado websockets

Rot*_*mir 8 tornado websocket node.js

我正在尝试构建基于websocket的应用程序.

我想知道是否可以向所有活动连接发送消息,因为它们是持久的.

假设我正在运行一个实时拍卖网站,并且我有多个用户正在观看拍卖页面,每个用户都通过套接字连接到我的服务器.现在让我们假设一个用户提高出价.我想向所有连接的客户端发送消息.最简单的方法是让客户端每秒通过套接字轮询服务器,但我认为websockets的想法是真正的双向通信.

如何才能做到这一点?

提前致谢,

ROTEM

And*_*rov 6

socket.io解决方案:

// note, io.listen() will create a http server for you
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  io.sockets.emit('this', { will: 'be received by everyone' });

  socket.on('private message', function (msg) {
    console.log('I received a private message from ', socket.id, ' saying ', msg);
    // Echo private message only to the client who sent it
    socket.emit('private message', msg);
  });

  socket.on('disconnect', function () {
    // This will be received by all connected clients
    io.sockets.emit('user disconnected');
  });
});

all_active_connections = {};
Run Code Online (Sandbox Code Playgroud)

webocket服务器(有很多),手动执行:

  var ws = require("ws");

  global_counter = 0;
  all_active_connections = {};

  ws.createServer(function (websocket) 
  {
      websocket.on('connect', function() 
      {
          var id = global_counter++;
          all_active_connections[id] = websocket;
          websocket.id = id; 
      }).on('data', function (data) {
          if (data == 'broadcast me!')
          {
              for (conn in all_active_connections)
                 all_active_connections[conn].write(data);
          }       
      }
    }).on('close', function() {
        delete all_active_connections[websocket.id];
    });
  }).listen(8080);
Run Code Online (Sandbox Code Playgroud)


Ton*_*leh 5

对于基于tornado/tornadio的解决方案,您的SocketConnection类需要在类级别维护一个连接列表。您的 on_connect 处理程序会将连接添加到此列表中,而 on_close 会将其删除。有关示例伪代码,请参阅Serge S. Koval 的这篇文章。代码重现如下:

声明您的 TornadIO 连接类:

class MyConnection(SocketConnection):
    participants = set()

    @classmethod
    def broadcast(cls, msg):
        for p in cls.participants:
            p.send(msg)

    @classmethod
    def controller_msg(cls, msg):
        cls.broadcast(msg)
Run Code Online (Sandbox Code Playgroud)

在您的设备轮询线程中,执行以下操作:

while True: 
    datum = file.readline() 
    if len(datum) > 2: 
        t = json.loads(datum) 
        ...
        def callback():
            MyConnection.controller_msg(t)

        io_loop.add_callback(callback)
Run Code Online (Sandbox Code Playgroud)

此外,gevent-socketio支持消息广播,但它基于 gevent,而不是tornado。

更新:

Tornadio2 已经维护了一个活动会话列表,因此您需要做的就是:

class MyConnection(SocketConnection):
    def broadcast(self, event, message):
        for session_id, session in self.session.server._sessions._items.iteritems():
            session.conn.emit(event, message)
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为每个连接实例都有一个对其会话的引用,该会话具有对用于创建应用程序的全局路由器的引用(存储为),该路由器在 中的对象server中维护会话列表。现在,每当您想在连接类中广播消息时,只需执行以下操作:SessionContainer_sessions

self.broadcast('my_custom_event', 'my_event_args')
Run Code Online (Sandbox Code Playgroud)