最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reconnect socket.io client on activity after auto-reconnect times out? - Stack Overflow

programmeradmin5浏览0评论

It is mon for laptops to go to sleep. This causes the socket.io client to disconnect. When the user returns to the web app, the socket.io client doesn't try to reconnect (probably reconnection limit reached?). How do I tell the socket to reconnect if the user does some action?

For example, I'd like the emit function to check if the connection is active, and if not then try to reconnect.

Note: I only need the client-side JS code and I'm not using node.js.

It is mon for laptops to go to sleep. This causes the socket.io client to disconnect. When the user returns to the web app, the socket.io client doesn't try to reconnect (probably reconnection limit reached?). How do I tell the socket to reconnect if the user does some action?

For example, I'd like the emit function to check if the connection is active, and if not then try to reconnect.

Note: I only need the client-side JS code and I'm not using node.js.

Share Improve this question edited Jul 27, 2012 at 2:32 Tony Abou-Assaleh asked Jul 26, 2012 at 10:39 Tony Abou-AssalehTony Abou-Assaleh 3,0402 gold badges27 silver badges38 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

In version 0.9 you could try to set the connect options object to some aggressive settings:

  var main = io.connect('/', {
    'reconnection delay': 100, // defaults to 500
    'reconnection limit': 100, // defaults to Infinity
    'max reconnection attempts': Infinity // defaults to 10
  });

Note that max reconnection attemps does not mean that the io client will stop to reconnect to the server after 10 failed attempts. If it was able to reconnect to the server 10 times and loses the connection for the 11th time, it will stop to reconnect.

See Socket.IO Configuration

Socket instances have an inner 'socket' property, which in turn has connect() method that you can call. It's normally called automatically when you instantiate the object (controlled by the 'auto connect' option). The inner socket also have a boolean "connected" property which you can check if you're not sure what the state is. Like so:

sio = io.connect();
//... time passes ...
if (! sio.socket.connected) {
  sio.connect();
}

The connect() method checks to make sure that the socket isn't in the middle of trying to connect, though for some reason it doesn't check to see if it's already connected. Not sure what happens if you connect() an already-connected socket...

The source code for the client library is fairly clear and well mented, which is good since the README on github doesn't provide a whole lot of help. It's handy to use an un-minified version of the library while you're doing development so you can dig into the source.

You could try to use the connect.failed event:

socket.on('connect_failed', function () {
   /* Insert code to reestablish connection. */
});

You could have a setInterval running which checks connected status like

s.connect()

if (! s.socket.connected) {
   s.connect();
发布评论

评论列表(0)

  1. 暂无评论