问题描述
我使用Socket.io在我的Node.js服务器和客户端网站之间进行通信。一切工作伟大!除了一件事。
I'm using Socket.io to communicate between my Node.js server and the client website. Everything works great! Except one thing.
我允许用户从Socket.io断开连接,以后重新连接,或至少我试图。记住,我的套接字从调用io()存储在mySocket,断开连接工作正常使用 mySocket.io.disconnect()
。服务器捕获它,客户端知道它已断开连接,这是伟大的。问题是当我尝试以后重新连接...
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...
所以一开始我只是试图设置 mySocket = io $ c>,但是失败了。我学习了一个重新连接的方法,所以我试着使用它,我的问题变得更糟。所以现在我使用
mySocket.io.reconnect()
,我得到一些奇怪的结果,如下所述。
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.
所以首先,服务器得到重新连接尝试。它捕获 on('connection',...)
事件就好了。但是,CLIENT似乎认为它从不重新连接 - 例如, mySocket.connected
的值保持在 false
并且所有 emit()
调用失败(并且永远不会到达服务器)。
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).
除了简单地使用 mySocket.io.reconnect()
?或者一些变量或参数我应该设置?为什么我的服务器捕获重新连接,如果客户端认为它没有连接?
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?
推荐答案
mySocket = io('/',{ forceNew:true})
在类似的情况下帮助了我。
mySocket = io('/', {forceNew: true})
helped me in similar situation.
这篇关于Socket.io客户端重新连接不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!