如何处理Socket ?IO房间与集群

How can I Handle Socket.IO rooms with cluster?

本文关键字:房间 IO Socket 何处理 处理      更新时间:2023-09-26

我有一个服务器与集群一起工作,并使其与套接字一起工作。IO我正在使用sticky-session,但是我的房间有一个问题(我不知道我这样做是否是最好的选择):集群正在实例化进程,每个进程都有特定数量的房间。

  • 服务器
    • 过程1
      • Room1
      • Room2
      • 房间N
    • 过程2
      • Room1
      • Room2
      • 房间N

我将一些用户连接到房间(只有一个进程)的方式是使用路由,用户访问页面,当他试图与Socket建立连接时。我检查URL并根据该信息将他插入房间。

我的问题是实现这个服务器与集群我不能插入用户在特定的房间,因为有一些房间只存在于特定的进程和粘会话把他在另一个进程。我如何将用户放在另一个进程的房间中?另外,用户只能看到进程的路由,他在服务器上,我想在页面上显示每个房间。

我已经读过Redis-Adapter,但我没有在github上找到使用Socket的解决方案。io + Cluster(Sticky-session + redis-adapter) + rooms.

跟随我的代码分享我所做的:

//Cluster.Master with simplified Code
if (cluster.isMaster) {
   var workers = [];
   // Spawn workers.
   for (var i = 0; i < num_processes; i++) {
      spawn(i);
   }
   // Create the outside facing server listening on our port.
   var server = net.createServer({
        pauseOnConnect: true
   }, function(connection) {
        // We received a connection and need to pass it to the appropriate
        // worker. Get the worker for this connection's source IP and pass
       // it the connection.
       var worker = workers[worker_index(connection.remoteAddress, num_processes)];
       worker.send('sticky-session:connection', connection);
   }).listen(process.env.PORT);
} else {
     console.log('I am worker #' + cluster.worker.id);
     var app = new express();
     //view engine
     app.set('views', './views');
     app.set('view engine', 'pug');
     //statics
     app.use(express.static(path.join(__dirname, 'public')));
     //rooms
     app.use('/', rooms);
     var server = app.listen(0, 'localhost'),
         io = sio(server);
     io.adapter(sio_redis({ host: 'localhost', port: 6379 }));
    //This File has the socket events (socket.on('messageX', function(){}))
    // And there I am 
    var realtime = require('./realtime/socketIOEvents.js')(io);
    // Listen to messages sent from the master. Ignore everything else.
    process.on('message', function(message, connection) {
    if (message !== 'sticky-session:connection') {
        return;
    }
   // Emulate a connection event on the server by emitting the
   // event with the connection the master sent us.
   server.emit('connection', connection);
   connection.resume();
});
}

socket -redis是正确的做法。每个服务器/进程在redis队列中侦听一个主题。简而言之,socketio-redis只是将事件发布到集群中的每个其他服务器/进程。因此,就房间而言,它们只是一组对房间中的消息感兴趣的套接字的抽象。

即使套接字分布在不同的服务器/进程中,它们也可以是同一个房间的一部分。当每条消息传入时,每个服务器都知道它并将其传递给所需的套接字。

就正确性而言,您的体系结构是正确的,因为您的代理决定也有选择地转发消息,但它增加了消息生命周期中的跳数。你真的不需要这个代理来处理套接字路由。

这是基于房间连接的示例代码。

http://www.html5gamedevs.com/topic/12321-create-a-cluster-server-with-nodejs-and-socketio/

如果你在这篇文章中有问题,请告诉我。

经过多次尝试,我在工人的顶部使用代理服务器,每个工人在另一个服务器(Redis)中注册房间。当我用房间的URL连接到服务器时,我的代理服务器检测到我试图连接的工作线程,并将我的web套接字连接路由到正确的工作线程。

                      Proxy
           /            |          '
worker(5rooms)   worker(5rooms)  worker(5rooms)
           '            |          /
                      Redis

这解决了我的问题,但它不使用socket.io-redis。我们如何用socket.io-redis解决这个问题?添加每个用户,套接字和变量在redis,使只是在工人的处理是一个有效的方法?