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

17,495

Solution 1

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.

Solution 2

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

enter image description here

Share:
17,495
modernator
Author by

modernator

Hello, I'm .modernator. My hobby is studying computer science, theorem and programming. I'm fullstack developer who lived in South Korea, Busan City. You can visit my personal blog: http://modernator.me or my personal company web site http://team.modernator.me . Any questions or contacts are welcome. Please don't hesitate me text: [email protected]

Updated on June 18, 2022

Comments

  • modernator
    modernator almost 2 years

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

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