调用.disconnect()后如何重新连接

问题:在发布手动.disconnect()之后,如何将客户端重新连接到服务器?

在我当前的项目中,当用户从会话中注销时,我需要断开客户端与服务器的连接。 我做了一个socket.disconnect()来成功地断开连接。 服务器从会话中删除用户。

过了一会儿,用户决定再次login,但是socket.io拒绝连接。

我很清楚Socket.IO已经实现了重连algorithm,但显然这是一个不同的情况。

下面是我做连接的一段代码。 在这个代码块的第二个触发器上,创build了对象socket ,但是没有connect被这个对象触发。

 //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'); }); 

更新:按照@Tony的要求

实际上整个事情都被Sencha Touch 2.0包裹了,但是我相信与ST2.0没有任何关系

这是我的数据类。 这个类的用法是当用户login时,这个类将被初始化。 在用户注销后,系统将调用该类中的disconnect()方法。

当用户再次login时,这个类再次被初始化,但是有趣的是套接字以某种方式保留了之前的所有事件和会话。

 /** * 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); } }); 

以下是我的console.log结果:

控制台截图

下面是我的node.jsdebugging窗口

Node.js调试窗口

我相信这个有趣的场景的根本原因是以前连接的connect事件监听器没有彻底删除。 我应该如何删除它? 我应该使用once吗? 或者我应该指定侦听器函数。 我认为removeAllListeners()就足够了这个任务。

最新的socket.io中的标准方法是:

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

这是我一直在我的应用程序中使用和伟大的作品。 它还确保了如果服务器正常工作,套接字不断尝试重新连接,并在服务器恢复联机时最终重新连接。

在你的情况下,你需要确保两件事情:

  1. 您只能创build一次套接字。 不要socket = io.connect(...)调用socket = io.connect(...)
  2. 你只能设置你的事件处理一次 – 否则它们会被多次触发!

所以当你想重新连接客户端时,调用socket.socket.reconnect() 。 您也可以在FireFox和Chrome的浏览器控制台中进行testing。

您可以通过以下客户端configuration重新连接。

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

在我看来,这是你如何处理断开连接…使用socket.io – v1.1.0为我工作

错误的方法…

 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 

正确的方法…

 // 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. 

我正在用socket.io 1.4.5这样做,现在它似乎工作:

 var app = { socket: null, connect: function() { // typical storing of reference to 'app' in this case var self = this; // reset the socket // if it's 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(); 
 socket.socket.connect(); 

让你重新连接。