1

这是使用https://github.com/fent/socket.io-clusterhub为每个集群进程执行的:

var store = new (require('socket.io-clusterhub'));
var io = require('socket.io').listen(config.ports.socket);
io.configure(function(){
    io.set('store',store);
});
console.log('Socket service listening on port '+config.ports.socket);
io.sockets.on('connection',function(socket){
    if(typeof fn === 'function'){fn(socket);}
});

但看起来所有 4 个线程都在尝试使用不同的套接字 id 注册同一个用户:

debug: client authorized
info: handshake authorized XEGx6zAP7rSWGQcaxmUB
debug: client authorized
info: handshake authorized r0Vcr7Z-r_9MnylyxmUC
debug: client authorized
info: handshake authorized 7RWUgoCuZPIu0IyQxmUj
debug: client authorized
info: handshake authorized mq0zKCgeDkisr-rbxmUk

然后它完全断开连接:

debug: setting request GET /socket.io/1/websocket/XEGx6zAP7rSWGQcaxmUB
debug: set heartbeat interval for client XEGx6zAP7rSWGQcaxmUB
debug: websocket writing 7:::1+0
warn: client not handshaken client should reconnect
info: transport end (error)
debug: set close timeout for client XEGx6zAP7rSWGQcaxmUB
debug: cleared close timeout for client XEGx6zAP7rSWGQcaxmUB
debug: cleared heartbeat interval for client XEGx6zAP7rSWGQcaxmUB
debug: discarding transport
debug: setting request GET /socket.io/1/websocket/r0Vcr7Z-r_9MnylyxmUC
debug: set heartbeat interval for client r0Vcr7Z-r_9MnylyxmUC
debug: setting request GET /socket.io/1/websocket/7RWUgoCuZPIu0IyQxmUj
debug: set heartbeat interval for client 7RWUgoCuZPIu0IyQxmUj
debug: websocket writing 7:::1+0
warn: client not handshaken client should reconnect
info: transport end (error)
debug: set close timeout for client 7RWUgoCuZPIu0IyQxmUj
debug: cleared close timeout for client 7RWUgoCuZPIu0IyQxmUj
debug: cleared heartbeat interval for client 7RWUgoCuZPIu0IyQxmUj
debug: discarding transport

它根本无法运行回调例程,即使它继续像r0Vcr7Z-r_9MnylyxmUC 仍然处于活动状态一样:

debug: cleared heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: set heartbeat interval for client r0Vcr7Z-r_9MnylyxmUC
debug: emitting heartbeat for client r0Vcr7Z-r_9MnylyxmUC
debug: websocket writing 2::
debug: set heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: got heartbeat packet
debug: cleared heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: set heartbeat interval for client r0Vcr7Z-r_9MnylyxmUC
debug: emitting heartbeat for client r0Vcr7Z-r_9MnylyxmUC
debug: websocket writing 2::
debug: set heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: got heartbeat packet
debug: cleared heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: set heartbeat interval for client r0Vcr7Z-r_9MnylyxmUC
debug: emitting heartbeat for client r0Vcr7Z-r_9MnylyxmUC
debug: websocket writing 2::
debug: set heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: got heartbeat packet
debug: cleared heartbeat timeout for client r0Vcr7Z-r_9MnylyxmUC
debug: set heartbeat interval for client r0Vcr7Z-r_9MnylyxmUC
debug: emitting heartbeat for client r0Vcr7Z-r_9MnylyxmUC

有任何想法吗?谢谢!

4

1 回答 1

1

由于 socket.io 通常使用长轮询与服务器通信,因此每个新请求都可以由集群中的任何节点处理。因此,每个节点都需要具有客户端的服务器状态才能响应请求。主要问题是在初始握手期间,socket.io 需要在创建有效会话之前发出少量请求,并且它们可以分布在集群中,这增加了代码的复杂性。Clusterhub 使用跨进程消息同步所有内容的方法在我看来并不高效。我建议查看粘性会话方法,保证每个客户端始终使用相同的节点。您可以在 nodejs 服务器前配置负载均衡器,或尝试类似https://github.com/indutny/sticky-session

于 2013-01-27T18:14:43.713 回答