Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

node.js - Controlling the heartbeat timeout from the client in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...