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

查看:30
本文介绍了使用 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.

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

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 分钟的超时时间.您可以使用 about: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天全站免登陆