How to reconnect after calling .disconnect ()

Question:. How do you reconnect the client to the server after you have issued the .disconnect() manual?

In my current project, I need to disconnect the client from the server when the user leaves the session. I did socket.disconnect() to do the disconnect. The server has deleted the user from the session.

After some time, the user decided to log in again, but socket.io refuses to connect.

It is clear that Socket.IO has implemented a reconnection algorithm, but it is clear that this is a different case.

Below is a snippet of code in which I am connecting. A socket object was created in the second trigger of this code block, but connect was not started from this object.

 //Start the socket var socket = io.connect(SOCKET_IO_URL); socket.on('connect', function() { me.fireEvent('connect'); socket.emit('register', { hashed_identifier: hashed_identifier, account_id: account_id }); }); socket.on('ready', function() { me.ready = true; me.log('Handshake done. Listening for new data'); }); socket.on('data', function(data) { me.fireEvent('data', data); //Tells server we received it socket.emit('data', {ack: 1}); }); socket.on('disconnect', function() { me.fireEvent('disconnect'); }); 

UPDATE: by @Tony request

In fact, all this is wrapped under Sencha Touch 2.0, but I believe that it has nothing to do with ST2.0

This is my data class. Using this class when a user logs in, this class will be initialized. And when the user logs out, the system will call the disconnect() method in this class.

When the user logs in again, this class is initialized again, but the funny socket somehow saved all the previous events and sessions that it previously had.

 /** * Serve as interface to wait for data communication in between server and client */ Ext.define('go.module.connect.Data', { mixins: { observable: 'Ext.mixin.Observable' }, config: { account: null }, ready: false, socket: null, constructor: function(cfg) { var me = this, hashed_identifier = Sha1.hash(go.__identifier); me.initConfig(cfg); var account_id = me.getAccount().get('id'); //Start the socket var socket = io.connect(SOCKET_IO_URL); socket.on('connect', function() { console.log('connect'); me.fireEvent('connect'); socket.emit('register', { hashed_identifier:hashed_identifier, account_id: account_id }); }); socket.on('ready', function() { me.ready = true; me.log('Handshake done. Listening for new data'); }); socket.on('data', function(data) { me.fireEvent('data', data); //Tells server we received it socket.emit('data', {ack: 1}); }); socket.on('disconnect', function() { me.fireEvent('disconnect'); }); console.log(socket); if (!socket.socket.connected){ socket.socket.reconnect(); } me.socket = socket; me.callParent([cfg]); }, disconnect: function() { this.socket.disconnect(); this.socket.removeAllListeners(); this.socket.socket.removeAllListeners(); }, log: function(msg) { console.log('@@ Connect: '+msg); } }); 

And below are my console.log results:

Console screenshot

And below is my node.js debug window

Node.js debug window

I believe that the main reason for this ridiculous scenario is that the previously attached connect event listener was not completely removed. How to remove it? Should I use once ? or should I specify a listener function. I thought removeAllListeners() enough for this task.

+6
source share
7 answers

The standard approach in the latest version of socket.io is:

 socket.on('disconnect', function() { socket.socket.reconnect(); } 

This is what I used in my application and it works great. It also ensures that the socket continues to try to reconnect if the server is moving forward, and eventually reconnects when the server is reconnected to the network.

In your case, you need to provide two things:

  • You create your socket only once. Do not call socket = io.connect(...) more than once.
  • You configure event processing only once, otherwise they will be triggered several times!

Therefore, when you want to reconnect the client, call socket.socket.reconnect() . You can also check this from the browser console in FireFox and Chrome.

+13
source

You can connect by performing the following configuration on the client side.

  // for socket.io version 1.0 io.connect(SERVER_IP,{'forceNew':true }; 
+2
source

I am doing this with socket.io 1.4.5 and it seems to work as long as:

 var app = { socket: null, connect: function() { // typical storing of reference to 'app' in this case var self = this; // reset the socket // if it not the first connect() call this will be triggered // I hope this is enough to reset a socket if( self.socket ) { self.socket.disconnect(); delete self.socket; self.socket = null; } // standard connectiong procedure self.socket = io.connect( 'http://127.0.0.1:3000', { // adapt to your server reconnection: true, // default setting at present reconnectionDelay: 1000, // default setting at present reconnectionDelayMax : 5000, // default setting at present reconnectionAttempts: Infinity // default setting at present } ); // just some debug output self.socket.on( 'connect', function () { console.log( 'connected to server' ); } ); // important, upon detection of disconnection, // setup a reasonable timeout to reconnect self.socket.on( 'disconnect', function () { console.log( 'disconnected from server. trying to reconnect...' ); window.setTimeout( 'app.connect()', 5000 ); } ); } } // var app app.connect(); 
+2
source

It seems to me that you are handling the disconnection ... Worked for me using socket.io - v1.1.0

Wrong Way...

 var sock = io( '/' ); sock.on( 'connect', function(x) { console.log( 'Connected', x ); } ); // now we disconnect at some point: sock.disconnect(); // now we try to reconnect... sock.connect() // or sock.reconnect(); // - both seem to create the new connection in the debugger, all events are missing though 

the right way...

 // As above with 3 extra characters, everything functions as expected... //events fire properly... sock.io.disconnect(); // consequently i'm also using (for consitency) sock.io.reconnect(); // sock.connect() still seems to work however. 
+1
source
 socket.socket.connect(); 

you will reconnect.

0
source

socket.open() works for me ...

  socket = io({ 'connect timeout': 500, 'browser client minification': true, 'browser client etag': true, 'force new connection': true, reconnection: true, reconnectionDelay: 500, reconnectionDelayMax: 500, reconnectionAttempts: Infinity, transports: ['websocket'] }) socket.open() 
0
source

socket.io-client v2.2.0

 import io from "socket.io-client" var socket = io(url) // initial connection const reconnect = () => { if(!socket.connected) { socket = io(url) // reconnects to a new socket } } 
0
source

Source: https://habr.com/ru/post/1400178/


All Articles