Javascript 如何在 socket.io 中配置重新连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11632969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How configure reconnecting in socket.io?
提问by Dmitro
I create socket.io connection with next code
我用下一个代码创建 socket.io 连接
var socket = new io.connect('http://localhost:8181', {
'reconnect': true,
'reconnection delay': 500,
'max reconnection attempts': 50
});
But when I kill server CTRL+Cand start it again, reconnection isn't happening. But disconnect event raised on client side. What maybe reason of it?
但是当我杀死服务器CTRL+C并重新启动它时,重新连接不会发生。但是在客户端引发了断开连接事件。可能是什么原因?
回答by Salvador Dali
This is an old question, but for other people like me, who are looking how to configure reconnect in socket.io (1.x)here is a correct syntax:
这是一个老问题,但对于像我这样正在寻找如何在socket.io (1.x) 中配置重新连接的其他人来说,这是一个正确的语法:
var socket = new io.connect('http://localhost:8181', {
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionDelayMax' : 5000,
'reconnectionAttempts': 5
});
回答by captainclam
I realise this is an old question, but I've been having some trouble with socket io reconnecting and found this post high in the search results, so thought I would contribue. Try debugging exactly which events are firing using the following code:
我意识到这是一个老问题,但我在重新连接 socket io 时遇到了一些麻烦,并在搜索结果中发现了这篇文章,所以我想我会做出贡献。尝试使用以下代码准确调试正在触发的事件:
# coffeescript. compile if you're writing javascript, obviously.
socket.on 'connect',-> console.log 'connected'
socket.on 'reconnect',-> console.log 'reconnect'
socket.on 'connecting',-> console.log 'connecting'
socket.on 'reconnecting',-> console.log 'reconnecting'
socket.on 'connect_failed',-> console.log 'connect failed'
socket.on 'reconnect_failed',-> console.log 'reconnect failed'
socket.on 'close',-> console.log 'close'
socket.on 'disconnect',-> console.log 'disconnect'
This should give you more insight into the state of the client socket.
这应该让您更深入地了解客户端套接字的状态。
Also, try looking in the Network tab of your web inspector to see if it is firing XHR requests as a fallback. Finally, in your web console, try typing io.sockets and expand it out to see whether it is actually trying to reconnect or not.
此外,尝试查看您的网络检查器的网络选项卡,看看它是否正在触发 XHR 请求作为后备。最后,在您的 Web 控制台中,尝试键入 io.sockets 并将其展开以查看它是否真的在尝试重新连接。
I have encountered problems with reconnect_failed not firing, and the reconnect tally not resetting. The following are links to discussions of these issues on github.
我遇到了 reconnect_failed 未触发和重新连接计数未重置的问题。以下是在 github 上讨论这些问题的链接。
reconnection delay - exponential back off not resetting properly
reconnect_failed gets never fired
回答by elmeltone
This is an old question, but I had the same question (for a different reason) when using v1.4.5. My chat room app worked beautifully, but when I hit Ctrl+C
in the terminal, my browser continued to loop and report ERR_CONNECTION_REFUSED every few seconds until I shut it down.
这是一个老问题,但我在使用 v1.4.5 时遇到了同样的问题(出于不同的原因)。我的聊天室应用程序运行良好,但是当我点击Ctrl+C
终端时,我的浏览器继续循环并每隔几秒钟报告 ERR_CONNECTION_REFUSED,直到我将其关闭。
Changing a previous answer just a bit gave me the solution.
稍微改变以前的答案就给了我解决方案。
For v1.4.5, here is my original code for "var socket" in my client js file:
对于 v1.4.5,这是我的客户端 js 文件中“var socket”的原始代码:
var socket = io();
And here is the solution:
这是解决方案:
var socket = io({
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionDelayMax' : 5000,
'reconnectionAttempts': 5
});
Obviously I could change the values if I want, but the important point is that this killed the never ending reconnection requests.
显然,如果我愿意,我可以更改这些值,但重要的是这会杀死永无止境的重新连接请求。
回答by MaX
reconnection delay is too small 500ms increase that, on top of that 50 retries means 500 * 50 = 25000 ms which is 25 seconds. If that doesn't help set a timeout on error event on client side to recreate the socket object (After error and some delay retry to create connection).
重新连接延迟太小 500 毫秒增加,在 50 次重试之上意味着 500 * 50 = 25000 毫秒,即 25 秒。如果这无助于在客户端设置错误事件超时以重新创建套接字对象(在错误和一些延迟后重试创建连接)。