Node.js和Socket.IO-如何在断开连接后立即重新连接
我正在用node.js和socket.io构建一个小型原型。一切正常,我面临的唯一问题是我的node.js连接将断开,我被迫刷新页面,以使连接重新启动并运行 是否有办法在触发断开连接事件后立即重新建立连接 据我所知,这是一个普遍的问题。因此,我正在寻找解决此问题的最佳实践方法:) 非常感谢,Node.js和Socket.IO-如何在断开连接后立即重新连接,node.js,Node.js,我正在用node.js和socket.io构建一个小型原型。一切正常,我面临的唯一问题是我的node.js连接将断开,我被迫刷新页面,以使连接重新启动并运行 是否有办法在触发断开连接事件后立即重新建立连接 据我所知,这是一个普遍的问题。因此,我正在寻找解决此问题的最佳实践方法:) 非常感谢, Dan编辑:Socket.io现在具有内置支持 当我使用socket.io时,断开连接没有发生(仅当我手动关闭服务器时)。但您可以在发生故障10秒或断开连接事件后重新连接 socket.on('discon
Dan编辑:Socket.io现在具有内置支持 当我使用socket.io时,断开连接没有发生(仅当我手动关闭服务器时)。但您可以在发生故障10秒或断开连接事件后重新连接
socket.on('disconnect', function(){
// reconnect
});
我提出了以下实施方案:
客户端javascript
var connected = false;
const RETRY_INTERVAL = 10000;
var timeout;
socket.on('connect', function() {
connected = true;
clearTimeout(timeout);
socket.send({'subscribe': 'schaftenaar'});
content.html("<b>Connected to server.</b>");
});
socket.on('disconnect', function() {
connected = false;
console.log('disconnected');
content.html("<b>Disconnected! Trying to automatically to reconnect in " +
RETRY_INTERVAL/1000 + " seconds.</b>");
retryConnectOnFailure(RETRY_INTERVAL);
});
var retryConnectOnFailure = function(retryInMilliseconds) {
setTimeout(function() {
if (!connected) {
$.get('/ping', function(data) {
connected = true;
window.location.href = unescape(window.location.pathname);
});
retryConnectOnFailure(retryInMilliseconds);
}
}, retryInMilliseconds);
}
// start connection
socket.connect();
retryConnectOnFailure(RETRY_INTERVAL);
编辑:socket.io现在具有。用那个 e、 g.(这些是默认值): 这就是我所做的:
socket.on('disconnect', function () {
console.log('reconnecting...')
socket.connect()
})
socket.on('connect_failed', function () {
console.log('connection failed. reconnecting...')
socket.connect()
})
虽然我只在websocket传输上测试过它,但它似乎工作得很好。即使第一次尝试失败,也要开始重新连接
如果第一次连接尝试失败,socket.io 0.9.16出于某种原因不会尝试重新连接。我就是这样解决的
//if this fails, socket.io gives up
var socket = io.connect();
//tell socket.io to never give up :)
socket.on('error', function(){
socket.socket.reconnect();
});
我知道这是一个公认的答案,但我一直在寻找我想要的,并认为这可能会帮助其他人 如果您想让您的客户端尝试无限期地重新连接(对于一个很少连接客户端的项目,我需要这样做,但是如果我关闭服务器,我需要它们始终重新连接)
只要重新连接插座就好了。另外,您从未设置过
超时@Nornagon I重新连接到套接字?同样,在您的实现中,您只需再次尝试连接到套接字。但是你断开连接是有原因的吗?@Alfred-你重新加载了整个网页。我确实因为某种原因断开了连接,但我仍想继续重试,直到该原因消失(即服务器重新启动:)@Nornagon我不会重新加载整个网页。你用过密码吗?我使用简单的ajax请求来测试webservice的可用性!window.location.href=unescape(window.location.pathname)
做了什么?我已经扩展了我的答案,添加了一个示例,并将链接改为指向socket.io客户端
存储库。这里还有其他文档。文档记录在谢谢,这一个适合我(但我使用socket.socket.connect();而不是重新连接)!“重新连接尝试”:“无限”
socket.on('disconnect', function () {
console.log('reconnecting...')
socket.connect()
})
socket.on('connect_failed', function () {
console.log('connection failed. reconnecting...')
socket.connect()
})
//if this fails, socket.io gives up
var socket = io.connect();
//tell socket.io to never give up :)
socket.on('error', function(){
socket.socket.reconnect();
});
var max_socket_reconnects = 6;
var socket = io.connect('http://foo.bar',{
'max reconnection attempts' : max_socket_reconnects
});
socket.on("reconnecting", function(delay, attempt) {
if (attempt === max_socket_reconnects) {
setTimeout(function(){ socket.socket.reconnect(); }, 5000);
return console.log("Failed to reconnect. Lets try that again in 5 seconds.");
}
});