gpt4 book ai didi

node.js - Socket.io Clusterhub 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:09 24 4
gpt4 key购买 nike

这是使用 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

有什么想法吗?谢谢!

最佳答案

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

关于node.js - Socket.io Clusterhub 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com