socket.io client reconnect timeout

我们两清 提交于 2020-05-25 07:25:13

问题


I'm using socket.io with node.js and I like the solution. The only issue I notice is around a disconnection and reconnection.

These are my current settings:

  'connect timeout': 1000,
  'reconnect': true,
  'reconnection delay': 300,
  'max reconnection attempts': 10000,
  'force new connection':true

I notice if I stop and start the node.js process the client connects back fine and quickly, however if the server is offline for a couple of minutes the client either never reconnects or takes a very long (non-user friendly) amount of time to.

I wanted to ask if there is anything I've missed or could add to the socket.io configuration to keep the client polling for a reconnection.

I know 'reconnection delay':

reconnection delay defaults to 500 ms

The initial timeout to start a reconnect, this is increased using an exponential back off algorithm each time a new reconnection attempt has been made.

But the exponential effect its not very user friendly. Is there a way to keep checking for a connection every X period of time - eg: 5 seconds.

If not I guess I can write some client side JS to check the connect and attempt reconnections if needed but it would be nice if the socket.io client offered this.

thx


回答1:


EDIT: See answer below for proper approach

I am afraid that the reconnection algorithm cannot be modified (as of December 2013); the Github issue to allow this feature is not merged yet. However, one of the commentors is suggesting a small workaround, which should nullify the exponential increase:

socket.socket.reconnectionDelay /= 2 on reconnecting

The other approach is, as you said, to write some client-side code to overwrite the reconnecting behavior, and do polling. Here is an example of how this could be done.

EDIT: the above code will have to go inside the 'disconnect' event callback:

var socket = io('http://localhost');
socket.on('connect', function(){
  socket.on('disconnect', function(){
      socket.socket.reconnectionDelay /= 2;
  });
});



回答2:


There is a configuration option, reconnection limit (see Configuring Socket.IO):

reconnection limit defaults to Infinity

  • The maximum reconnection delay in milliseconds, or Infinity.

It can be set as follows:

io.set("reconnection limit", 5000);

When set, the reconnection delay will continue to increase (according to the exponential back off algorithm), but only up to the maximum value you specify.




回答3:


This is the solution I went with - when the socket disconnects it enters a loop that keeps trying to reconnect every 3 seconds until the connection is created - works a treat:

  socket.on('disconnect', function() {
    socketConnectTimeInterval = setInterval(function () {
      socket.socket.reconnect();
      if(socket.socket.connected) {clearInterval(socketConnectTimeInterval);}
    }, 3000);
  });

cheers



来源:https://stackoverflow.com/questions/20558290/socket-io-client-reconnect-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!