I'm using Socket.io to communicate between my Node.js server and the client website. Everything works great! Except one thing.
I'm allowing the user to disconnect from Socket.io and reconnect later, or at least I'm trying to. Keeping in mind that my socket from calling io() is stored in mySocket, disconnecting works fine using mySocket.io.disconnect()
. The server catches it, the client knows it's disconnected, it's great. The issue is when I try to reconnect later...
So at first I just tried to set mySocket=io()
again, but that failed. I learned there's a reconnect method, so I tried using that, and my problems got worse. So now I'm using mySocket.io.reconnect()
, and I'm getting some weird results, described below.
So first of all, the server IS getting the reconnection attempt. It's catching the on('connection',...)
event just fine. However, the CLIENT seems to think it's never reconnected--for example, the value of mySocket.connected
stays at false
and all emit()
calls fail (and never get to the server).
Is there some special method I'm supposed to use besides simply mySocket.io.reconnect()
? Or some variable or argument I'm supposed to set? Why is my server catching the reconnect if the client thinks it's not connected? What's going on and how do I fix it?