简体   繁体   中英

socket.io 'connect' or 'reconnect' event not fired after reconnect

i have a problem with using Socket.io.

code is simple:

var socket = null;
var socketInit = false;    //if it is true, use reconnect

...
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.connect();
    }
}

...
function disconnect() {
    socket.disconnect();
}

as you can see, there are two functions that connect or disconnect socket. and in connect function, it attachs 'connect', 'disconnect', 'reconnect' handlers to the socket.

first i called connect function, it works well. server side is also working well. and i called disconnect function, works well too so as the server.

but i called connect function again, that means "socketInit" variable is true, so connect function tries:

socket.io.connect();

i checked the server and client console both, and looks like it has connected, but the problem is the events are not fired!

server side 'connection' event was fired on reconnection but client side 'connect' or 'reconnect' event wasn't fired. i tried to figure out what is going on, but still i don't get it. i checked web console and server console many times, and the connection itself is working fine, but the problem is events are not working!

if someone know the way, it will be very appreciated to help me. thanks ;)

ps i tried to add 'reconnected', 'reconnection', 'reconnect_error' event handlers, but neither not works.

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);

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM