Marco Madueño
Marco Madueño

Reputation: 1

Socket.io: IE fires reconnecting event only once

I have an app with node.js/socket.io running fine. Every time a client looses connection, it tries to reconnect using socket.io predefined process for that:

var io = io.connect("/", {
    'reconnect': true,
    'reconnection delay': 1000,
    'reconnection limit': 5000,
    'max reconnection attempts': 100
});

This works fine for all browsers except Internet Explorer. I added some alerts on all reconnecting events to check where the problem might be:

io.on('disconnect', function(){ alert('Disconnected!'); });
io.on('reconnect_failed', function(){ alert('FAILED'); });
io.on('reconnect', function(){ alert('OK'); });
io.on('reconnecting', function( nextRetry ){ alert('Reconnecting...'); });

When I stop socket.io server (or client connection is lost), all clients get the 'on disconnect' message. No problem with that.

Clients with other than IE

All clients keep getting the 'Reconnecting...' message every 5 seconds until connection has been restored, and if so, they get the final OK message.

Clients with IE

If server is still down by the time IE tries to reconnect (the first time), client does not continue trying to reconnect, and even when server is up again users will never be connected until refresh of page. Since disconnect event, clients get the "Reconnecting..." message only once.

Just as a plus test, if server is restored before IE client tries to reconnect, then client get reconnected with success.

I found this problem here: https://github.com/LearnBoost/socket.io/issues/1020. But they to not come to a solution. I serve the socket.io.js file dinamically, so I cannot edit it the way they suggest.

Thanks!

Upvotes: 0

Views: 828

Answers (1)

Aleksandr Guidrevitch
Aleksandr Guidrevitch

Reputation: 459

This is how I worked around this issue for socket.io 0.9.6 / ie8:

  var _socket = io.connect();
  ...
  _socket.on('disconnect', function(){
      var interval = setInterval(function () {
          if (_socket.socket.connected) {
              clearInterval(interval);
          } else {
              _socket.socket.disconnect();
              _socket.socket.reconnect();
          }
      }, 31000);
  });

31 seconds interval is needed for the previous jsonp script inserted into document to time out, othervise multiple connect events are triggered (multiple sockets get connected) when server becomes available..

Upvotes: 0

Related Questions