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:

And below is my 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.