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

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

问题描述

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

  • A型

    由于网络问题导致连接中断时(或 客户端崩溃)默认的心跳超时时间太长

  • B型

    当此客户端的连接断开时,我需要给它更多 恢复时间-客户恢复比 服务器中断连接/会话

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

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

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

为单个客户端更改heartbeat timeout很容易:

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

但是,在服务器上,heartbeat interval值似乎是共享库的一部分(管理器,这是您从var io = require("socket.io").listen(server)调用中获得的值),这意味着无法轻松地将其更改为单个插座.

我敢肯定,通过一些socket.io黑客攻击,您应该能够实现它,但是您可能会在此过程中破坏其他内容...

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

  • 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

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

解决方案

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.

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

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.

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天全站免登陆