使用websockets处理连接丢失 [英] Handling connection loss with websockets

查看:217
本文介绍了使用websockets处理连接丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近设置了一个本地WebSocket服务器工作正常但是我有一些麻烦,了解我应该如何处理客户端或服务器故意启动的突然断开连接,即:服务器断电拉出以太网电缆等...我需要客户端知道连接是否在〜10秒内丢失。

I've recently set-up a local WebSocket server which works fine, however I'm having a few troubles understanding how I should handle a sudden loss of connection which neither the client or server intentionally initiated, i.e: Server loses power, ethernet cables pulled out etc... I need the client's to know whether connection has been lost within ~10seconds.

客户端,连接简单:

var websocket_conn = new WebSocket('ws://192.168.0.5:3000');

websocket_conn.onopen = function(e) {
    console.log('Connected!');
};

websocket_conn.onclose = function(e) {
    console.log('Disconnected!');
};

我可以手动触发连接断开连接正常,

I can manually trigger the connection disconnect which works fine,

websocket_conn.close();

但如果我只是将以太网线从计算机背面拔出,或者禁用了连接, onclose 未被调用。我在另一篇文章中读到,当 TCP检测到连接丢失,但是我不需要及时作为Firefox的默认设置我认为是10分钟,而且我真的不想绕过数百台计算机 about:config 更改此值。我读过的唯一其他建议是使用'ping / pong'保持活动的轮询样式方法,这似乎与websockets的想法相反。

But if I simply pulled the ethernet cable out the back of the computer, or disabled the connection, onclose doesn't get called. I've read in another post that it would eventually get called when TCP detects loss of connectivity, but it's not in the timely manner that I need as the default for Firefox I believe is 10 minutes, and I don't really want to go around hundreds of computers about:config changing this value. The only other suggestion I've read is to use a 'ping/pong' keep-alive polling style method which seems counterintuitive to the idea of websockets.

是否有更简单的方法来检测这种断开行为?从技术角度看,我正在阅读的旧帖子是否仍然是最新的,最好的方法仍然是'ping / pong'风格?

Is there an easier way to detect this kind of disconnect behaviour? Are the old posts i'm reading still up to date from a technical point, and the best method is still 'ping/pong' style?

推荐答案

这是我最终做的解决方案,目前似乎工作正常,它完全针对我的项目的设置和安装。依赖于我的问题中最初未提及的标准,但如果他们碰巧做同样的事情,它可能对其他人有用。

This was the solution I ended up doing which seems to work fine for the time being, it's entirely specific to my project's setup & relies on criteria being in place that wasn't originally mentioned in my question, but it might be useful for someone else if they happen to be doing the same thing.

连接到websocket服务器发生在Firefox插件中,默认情况下,Firefox的TCP设置有10分钟超时。您可以使用关于:config 查看其他详细信息并搜索TCP。

The connection to the websocket server occurs within a Firefox addon, and by default Firefox's TCP setup has a 10 minute timeout. You can see additional details with about:config and searching for TCP.

Firefox插件可以访问这些参数

Firefox addons can access these parameters

var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);

并通过指定分支&更改这些参数。首选项以及新值

and also change these parameters by specifying the branch & preference along with the new value

prefs.getBranch("network.http.tcp_keepalive.").setIntPref('long_lived_idle_time', 10);

现在,任何安装了插件的计算机都有10秒的TCP连接超时。如果连接丢失,将触发 onclose 事件,该事件显示警报并尝试重新建立连接

So now, any computer with the addon installed have a 10 second timeout for TCP connections. If the connection is lost, the onclose event is triggered which displays an alert and also attempts to re-establish connection

websocket_conn.onclose = function (e) {
    document.getElementById('websocket_no_connection').style.display = 'block';
    setTimeout(my_extension.setup_websockets, 10000);
}; 

这篇关于使用websockets处理连接丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆