Socket.io client-side reconnect not working

怎甘沉沦 提交于 2020-01-24 23:30:23

问题


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?


回答1:


In Client sode:

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



来源:https://stackoverflow.com/questions/24886481/socket-io-client-side-reconnect-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!