The right way for a client to reconnect after the server does a socket.disconnect( true )
See original GitHub issueServer: node + socket.io
var io = require( 'socket.io' )( 3000 );
io.on( 'connection', function ( socket ) ...
// at some point if client auth fails, server may kick him out:
socket.disconnect( true );
Client: html/js + socket.io 1.4.5
socket = io.connect( 'http://127.0.0.1:3000', {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax : 5000,
reconnectionAttempts: 99999
} );
socket.on( 'connect', function () {
console.log( 'connected to server' );
} );
socket.on( 'disconnect', function () {
console.log( 'disconnected to server' );
} );
The above is simplified but that is the basis. Reconnection works fine if I fire up the client when the server is down. The client tries to connects to the server and when I finally fire up the server the connection is estabilished.
At some point it may happen that the server decides to disconnect the client, mainly because he logged out invalidating the token and a query on the db tells to do so, or the auth token expired or whatever.
IN this circumstance I’d like the restore the server polling by the client because the user may want to login again and he has the rights to connect again to the server.
If I refresh the page (client) in the browser it works okay but it’s an ugly solution.
I think the cause is the “Manager.skipReconnect” property set to true after doing disconnect(), so I’m asking what is the proper way to reinitiate a reconnection polling by the client after the server disconnects a client.
Found some answers on stackoverflow about this but all were unsuccessfull, all stuff to be executed in the client:
- socket.io.reconnect();
- socket.socket.reconnect(); // ??
- socket = io.connect( ‘http://127.0.0.1:3000’, … // reinvoking the connect snippet seems bad idea
Could you help me about this matter?
Thank you
Issue Analytics
- State:
- Created 8 years ago
- Reactions:27
- Comments:35 (1 by maintainers)
Top GitHub Comments
For now, I managed this way and it seems to be stable:
Client:
Use 0.8.3 version of io.socket remove new version 1.0.0