javascript 如何在自动重新连接超时后重新连接 socket.io 客户端的活动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11667516/
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 to reconnect socket.io client on activity after auto-reconnect times out?
提问by Tony Abou-Assaleh
It is common for laptops to go to sleep. This causes the socket.io client to disconnect. When the user returns to the web app, the socket.io client doesn't try to reconnect (probably reconnection limit reached?). How do I tell the socket to reconnect if the user does some action?
笔记本电脑进入睡眠状态是很常见的。这会导致 socket.io 客户端断开连接。当用户返回 Web 应用程序时,socket.io 客户端不会尝试重新连接(可能已达到重新连接限制?)。如果用户执行某些操作,如何告诉套接字重新连接?
For example, I'd like the emit
function to check if the connection is active, and if not then try to reconnect.
例如,我希望该emit
功能可以检查连接是否处于活动状态,如果没有,则尝试重新连接。
Note: I only need the client-side JS code and I'm not using node.js.
注意:我只需要客户端 JS 代码,我没有使用 node.js。
回答by django
In version 0.9 you could try to set the connect options object to some aggressive settings:
在 0.9 版本中,您可以尝试将连接选项对象设置为一些激进的设置:
var main = io.connect('/', {
'reconnection delay': 100, // defaults to 500
'reconnection limit': 100, // defaults to Infinity
'max reconnection attempts': Infinity // defaults to 10
});
Note that max reconnection attemps
does not mean that the io client will stop to reconnect to the server after 10 failed attempts. If it was able to reconnect to the server 10 times and loses the connection for the 11th time, it will stop to reconnect.
请注意,max reconnection attemps
这并不意味着 io 客户端在尝试 10 次失败后将停止重新连接到服务器。如果它能够重新连接到服务器 10 次并且第 11 次失去连接,它将停止重新连接。
回答by Dirk Bergstrom
Socket instances have an inner 'socket' property, which in turn has connect() method that you can call. It's normally called automatically when you instantiate the object (controlled by the 'auto connect' option). The inner socket also have a boolean "connected" property which you can check if you're not sure what the state is. Like so:
Socket 实例有一个内部的 'socket' 属性,它又具有可以调用的 connect() 方法。它通常在您实例化对象时自动调用(由“自动连接”选项控制)。内部套接字还有一个布尔值“已连接”属性,如果您不确定状态是什么,您可以检查它。像这样:
sio = io.connect();
//... time passes ...
if (! sio.socket.connected) {
sio.connect();
}
The connect() method checks to make sure that the socket isn't in the middle of trying to connect, though for some reason it doesn't check to see if it's already connected. Not sure what happens if you connect() an already-connected socket...
connect() 方法会检查以确保套接字不在尝试连接的过程中,但由于某种原因它不会检查它是否已经连接。不知道如果你 connect() 一个已经连接的套接字会发生什么......
The source code for the client library is fairly clear and well commented, which is good since the README on githubdoesn't provide a whole lot of help. It's handy to use an un-minified version of the library while you're doing development so you can dig into the source.
客户端库的源代码相当清晰,注释也很好,这很好,因为github上的 README没有提供很多帮助。在进行开发时使用库的未缩小版本很方便,因此您可以深入了解源代码。
回答by nfechner
You could try to use the connect.failed
event:
您可以尝试使用该connect.failed
事件:
socket.on('connect_failed', function () {
/* Insert code to reestablish connection. */
});
回答by Karan Checker
You could have a setInterval running which checks connected status like
您可以运行 setInterval 来检查连接状态,例如
s.connect()
if (! s.socket.connected) {
s.connect();