Web的应用程序。 NodeJS + Socket.io(仅限websockets,启用心跳)。
我的应用程序工作正常(它连接到Socket.io,它正确地发出/接收消息),但我检测到随机断开,而不是因为心跳超时。即使服务器从客户端收到心跳包,仍然可能在几秒钟内断开此客户端。我不明白原因。它几乎是随机发生的,大约每3-15分钟。更改socket.io配置似乎不会影响频率。
以下是日志,它清楚地表明断开连接的原因是不是心跳超时,而是传输端(套接字端):
日志:
debug: emitting heartbeat for client cTVCsv2GS2R_lh3Ecao-
debug: websocket writing 2::
debug: set heartbeat timeout for client cTVCsv2GS2R_lh3Ecao-
debug: got heartbeat packet
debug: cleared heartbeat timeout for client cTVCsv2GS2R_lh3Ecao-
debug: set heartbeat interval for client cTVCsv2GS2R_lh3Ecao-
info: transport end (socket end)
// ^ Why?
debug: set close timeout for client cTVCsv2GS2R_lh3Ecao-
debug: cleared close timeout for client cTVCsv2GS2R_lh3Ecao-
debug: cleared heartbeat interval for client cTVCsv2GS2R_lh3Ecao-
SOCK 25.12 22:23:20.675 disconnection from IO detected (USER1)
// ^ This means disconnected event fired
debug: discarding transport
debug: client authorized
info: handshake authorized GPBLHsqhhrdsTeqTcao_
debug: setting request GET /socket.io/1/websocket/GPBLHsqhhrdsTeqTcao_?conftoken=20519986772
debug: set heartbeat interval for client GPBLHsqhhrdsTeqTcao_
debug: client authorized for
debug: websocket writing 1::
debug: client authorized for /io/activity
debug: websocket writing 1::/io/activity
SOCK 25.12 22:23:23.005 reconnected to IO (USER1)
客户端:
socket = io.connect('/io/activity',{'max reconnection attempts':Infinity})
服务器:
io = require('socket.io').listen(server, {
log: true
, "close timeout": 120
, "heartbeat timeout": 120
, "heartbeat interval": 30
, "transports": ["websocket"]
})
io.enable('browser client minification')
io.enable('browser client etag')
io.enable('browser client gzip')
答案 0 :(得分:0)
如果我使用的是我不知道其内部工作的包,我通常会进行防御性编码。
几年前,当我在socke.io中出现随机错误时,我只是监听错误事件,然后重新启动连接。请注意,某些客户端的数据可能会丢失,因此处理错误。
看看socket.io错误会很有帮助。 祝你好运