在 socket.io 中从客户端控制心跳超时 [英] Controlling the heartbeat timeout from the client in socket.io

查看:52
本文介绍了在 socket.io 中从客户端控制心跳超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有移动客户端连接到 node.js 服务器,通过 xhr-polling 运行 socket.io.我有两种类型的客户:

I have mobile clients connected to a node.js server, running socket.io via xhr-polling. I have two type of clients:

  • A 型

当连接因网络问题(或客户端崩溃)默认心跳超时时间过长

When the connection breaks up due to network problems (or that the client crashes) the default heart beat timeout is too long

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

所以我的问题是如何配置(如果可能)来自实际客户端的心跳超时?

So my question is how to I configure (if it is possible) the heartbeat timeouts from the actual client?

推荐答案

据我所知,这里有 2 个重要的值:服务器每隔 心跳间隔 秒向客户端发送心跳;客户端直接响应,如果没有响应,服务器判断客户端已死.自上次心跳以来,客户端等待来自服务器的心跳 heartbeat timeout 秒(这显然应该高于 heartbeat interval).如果它在 heartbeat timeout 秒内没有收到来自服务器的消息,则假定服务器已死(并将根据您设置的其他选项开始断开连接/重新连接.

As far as I can tell, there are 2 values that matter here: the server sends heartbeats to the client every heartbeat interval seconds; 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 timeout seconds since the last heartbeat (which should obviously be higher than the heartbeat interval). If it hasn't received word from the server in heartbeat timeout seconds, it assumes the server is dead (and will start disconnecting / reconnecting based on the other options you have set.

默认值为 heartbeat interval = 25sheartbeat timeout = 60s.这两项都在服务器端设置,heartbeat timeout在连接时发送给客户端.

Default values are heartbeat interval = 25s and heartbeat timeout = 60s. Both items are set on the server, the heartbeat timeout is sent to the client upon connecting.

更改单个客户端的心跳超时很容易:

Changing the heartbeat timeout for a single client is easy:

var socket = io.connect(url);
socket.heartbeatTimeout = 20000; // reconnect if not received heartbeat for 20 seconds

但是在服务器上,heartbeat interval 值似乎是共享对象的一部分(Manager,这是您从 var io = require("socket.io").listen(server) 调用),这意味着它不能很容易地为单个套接字更改.

However on the server, the heartbeat interval value 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.

我确信通过一些 socket.io hacking 你应该能够实现它,但你可能会在这个过程中破坏其他东西......

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 中从客户端控制心跳超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆