问题:发布手动.disconnect()后如何将客户端重新连接到服务器
在我的当前项目中,当用户从会话中注销时,我需要断开客户端与服务器的连接。我做了一个socket.disconnect()来成功地断开连接。服务器将用户从会话中删除。
过了一会儿,用户决定再次登录,但socket.io拒绝连接。
我很清楚,Socket.IO已经实现了重连接算法,但这显然是另一种情况。
下面是我做连接的代码段。在这个代码块的第二个触发器上,创建了对象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无关
这是我的数据类。这个类的用法是当用户登录时,这个类将被初始化。在用户注销时,系统将调用该类中的disconnect()方法。
当用户再次登录时,该类将再次初始化,但有趣的是套接字以某种方式保留了以前的所有事件和会话。
/**
* 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.js调试窗口

我认为,这个有趣场景的根本原因是之前附加的connect 事件侦听器没有被彻底删除。我该怎么移除它?我应该使用once**?吗?或者我也应该指定侦听器函数。我认为** removeAllListeners() 足以完成这项任务。
发布于 2012-03-09 03:51:44
最新socket.io中的标准方法是:
socket.on('disconnect', function() {
socket.socket.reconnect();
})这是我在我的应用程序中一直在使用的东西,而且效果很好。它还确保如果服务器运行正常,套接字将继续尝试重新连接,并最终在服务器恢复联机时重新连接。
在您的情况下,您需要确保两件事:
socket = io.connect(...)。因此,当您想重新连接客户端时,请调用socket.socket.reconnect()。您还可以在FireFox和Chrome的浏览器控制台上测试这一点。
发布于 2014-11-24 14:54:39
您可以通过以下客户端配置重新连接。
// for socket.io version 1.0
io.connect(SERVER_IP,{'forceNew':true };发布于 2016-03-09 12:24:52
我在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();https://stackoverflow.com/questions/9598900
复制相似问题