node.js 在 socket.io 中从客户端控制心跳超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12815231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controlling the heartbeat timeout from the client in socket.io
提问by Martin
I have mobile clients connected to a node.js server, running socket.io via xhr-polling. I have two type of clients:
我有移动客户端连接到 node.js 服务器,通过 xhr-polling 运行 socket.io。我有两种类型的客户:
Type A
When the connection breaks up due to network problems (or that the client crashes) the default heart beat timeout is too long
Type B
When the connection breaks up for this client I need to give it more time to recover - it is more important that the client recovers than the server breaks the connection/session
A型
当连接因网络问题(或客户端崩溃)而中断时,默认心跳超时时间过长
B型
当这个客户端的连接中断时,我需要给它更多的时间来恢复 - 客户端恢复比服务器中断连接/会话更重要
So my question is how to I configure (if it is possible) the heartbeat timeouts from the actual client?
所以我的问题是如何配置(如果可能)来自实际客户端的心跳超时?
回答by Claude
As far as I can tell, there are 2 values that matter here: the server sends heartbeats to the client every heartbeat intervalseconds; the client responds directly, if there is no response, the server decides the client is dead. The client waits for a heartbeat from the server for heartbeat timeoutseconds since the last heartbeat (which should obviously be higher than the heartbeat interval). If it hasn't received word from the server in heartbeat timeoutseconds, it assumes the server is dead (and will start disconnecting / reconnecting based on the other options you have set.
据我所知,这里有两个重要的值:服务器每秒钟向客户端发送一次心跳heartbeat interval;客户端直接响应,如果没有响应,服务器判断客户端已死。heartbeat timeout自上次心跳以来,客户端等待来自服务器的心跳数秒(显然应该高于heartbeat interval)。如果它在heartbeat timeout几秒钟内没有收到来自服务器的消息,则假定服务器已死(并将根据您设置的其他选项开始断开连接/重新连接。
Default values are heartbeat interval = 25sand heartbeat timeout = 60s. Both items are set on the server, the heartbeat timeoutis sent to the client upon connecting.
默认值为heartbeat interval = 25s和heartbeat timeout = 60s。这两项都在服务器上设置heartbeat timeout,连接后发送到客户端。
Changing the heartbeat timeoutfor a single client is easy:
更改heartbeat timeout单个客户端的 很容易:
var socket = io.connect(url);
socket.heartbeatTimeout = 20000; // reconnect if not received heartbeat for 20 seconds
However on the server, the heartbeat intervalvalue seems to be part of a shared object (the Manager, which is what you get back from your var io = require("socket.io").listen(server)call), which means that it can't easily be changed for individual sockets.
然而,在服务器上,该heartbeat interval值似乎是共享对象的一部分(管理器,这是您从var io = require("socket.io").listen(server)调用中返回的内容),这意味着不能轻易更改单个套接字的值。
I'm sure that with some socket.io hacking you should be able to make it happen, but you might break other stuff in the process...
我确信通过一些 socket.io hacking 你应该能够实现它,但你可能会在这个过程中破坏其他东西......
回答by Joseph238
In socket.io 1.x, the API has changed but the concept remains the same.
在 socket.io 1.x 中,API 发生了变化,但概念保持不变。
Instead of heartbeatInterval and heartbeatTimeout, the properties are pingInterval and pingTimeout. For example,
属性不是 heartbeatInterval 和 heartbeatTimeout,而是 pingInterval 和 pingTimeout。例如,
var server = app.listen(80);
io = socketio(server,{'pingInterval': 45000});
See the documentation at http://socket.io/docs/server-api/#server(opts:object), which will direct you to https://github.com/Automattic/engine.io#methods-1.
请参阅http://socket.io/docs/server-api/#server(opts:object) 上的文档,它将引导您到https://github.com/Automattic/engine.io#methods-1。
回答by radu122
To complete Claude's answer, you can set these values at the server lever, with the following code
要完成克劳德的回答,您可以在服务器杠杆上设置这些值,使用以下代码
io.set('heartbeat interval', 5);
io.set('heartbeat timeout', 11);
and then you could create 2 node.js servers, one for each type of client (A and B), having 2 desired set of options (heartbeat timeout and interval).
然后您可以创建 2 个 node.js 服务器,每个类型的客户端(A 和 B)一个,具有 2 组所需的选项(心跳超时和间隔)。

