socket.io 'connect' or 'reconnect' event not fired after reconnect
If you're using Socket.io 1.0, try using the io manager on the socket to handle manual disconnection and reconnection.
var socket = null;
var socketInit = false;
function connect() {
if(!socketInit) {
socket = io();
socketInit = true;
//attach event handlers
socket.on('connect', function() {
console.log('connect fired!');
});
socket.on('disconnect', function() {
console.log('disconnect fired!');
});
socket.on('reconnect', function() {
console.log('reconnect fired!');
});
}
else {
socket.io.reconnect();
}
}
function disconnect() {
socket.io.disconnect();
}
The reconnecting_attempt, reconnecting, reconnected and connected events on the socket should all be emitted afterwards.
I am using socket.io 2.1.1, and here is all the event sequence that when socket.io handle reconnect after disconnected:
Just do socketClient.on(eventName, callBackFunc);