2

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?

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26

1 Answers1

2

In Client sode:

mySocket = io('/', {forceNew: true}) helped me in similar situation.

Xin
  • 33,823
  • 14
  • 84
  • 85
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 1
    Awesome, thanks! It works! I saw something like that, but they had the option key name as, in quotes, "force new connection"--clearly not right. The only thing I'm worried about here is this: if I set mySocket=io(...) after it's been disconnected, will the old socket be cleaned up? I'd like to avoid memory leaks with unused sockets still floating around if I can. – IceMetalPunk Jul 22 '14 at 13:18
  • 1
    @IceMetalPunk 'force new connection' -- is an option for Socket.IO 0.9. Since 1.0 they have changed their API. Not sure about memory leaks. Never asked this question to myself :) If you wish, you can explore Socket.IO source code, it's well written, and it would be a great experience. – Oleg Jul 22 '14 at 13:26
  • Ah, so what I found on Google were some old, out-of-date docs. Fair enough. I'll look into the source of Socket, see if I can spot any references that wouldn't be cleaned up by nullifying the mySocket variable before a reconnect. Thanks for all your help! :D – IceMetalPunk Jul 22 '14 at 13:33