admin管理员组

文章数量:1278985

Is something like this possible?

socket.on('disconnect', function(){
    console.log('disconnected...');
    socket.connect();
    socket.on('connect', function(){
    console.log('...reconnected');
    })  
})

Is something like this possible?

socket.on('disconnect', function(){
    console.log('disconnected...');
    socket.connect();
    socket.on('connect', function(){
    console.log('...reconnected');
    })  
})
Share Improve this question edited May 5, 2011 at 14:22 davin 45.6k9 gold badges80 silver badges78 bronze badges asked May 5, 2011 at 13:18 fancyfancy 51.5k64 gold badges157 silver badges230 bronze badges 2
  • Most likely you already have an on "connect" event outside of disconnect so make sure not to duplicate the event. – Detect Commented May 5, 2011 at 15:28
  • Yes, I do, just added it for the purpose of this question. Thanks – fancy Commented May 5, 2011 at 16:28
Add a ment  | 

1 Answer 1

Reset to default 12

Socket.io reconnects automatically (if you set the reconnect option, although it defaults to true), so you don't really need to do that.

Furthermore, there is a reconnect event which seems far more appropriate.

Also, set your event handlers independently, don't set the connect handler in the execution of the disconnect handler.

本文标签: javascriptSocketio reconnect on disconnectStack Overflow