我如何知道连接是否存活websockets? [英] How do i know if connection is alive with websockets?

查看:84
本文介绍了我如何知道连接是否存活websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个webapp,它运行在浏览器中。该webapp连接到使用websockets的服务器。所以服务器和我的客户端/浏览器之间的通信基于websockets。如果服务器上发生了一些魔法事件,一些webservice会向我的webapp发送一个新的XML / JSON,并显示新的数据。

I have a webapp, which is running in a browser. That webapp is connected to a server, which uses websockets. So the communication between the server and my client/browser is based on websockets. If some magic event occurs on the server, some webservice sends a new XML / JSON to my webapp and the new data gets displayed.

但是, / browser,知道连接是否存活?让我说我没有得到任何新的XML约30秒。我如何知道连接是关闭/损坏/服务器离线还是一切正常,但在服务器本身没有发生新的魔术事件。

But how do i, as the client / browser, know if the connection is stil alive? Lets say i do not get any new XML for about 30 seconds. How would i know if the connection is closed/broken/server offline or everything is fine, but on the server himself no new magic event occured.

推荐答案

3种方式:


  • 依靠TCP来检测连接丢失,最终会在JS onclose事件中弹出

  • 从服务器发送WebSocket ping。浏览器将以WS pongs回复,连接失败在客户端也可能更加鲁棒。

  • 发送应用级心跳从浏览器到服务器,服务器需要有逻辑回复。您不能从浏览器(在JS中)触发WS ping。

这篇关于我如何知道连接是否存活websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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