检测互联网连接是否离线? [英] Detect the Internet connection is offline?

查看:38
本文介绍了检测互联网连接是否离线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JavaScript 中检测互联网连接是否离线?

How to detect the Internet connection is offline in JavaScript?

推荐答案

您可以通过发出失败的 XHR 请求来确定连接丢失.

You can determine that the connection is lost by making failed XHR requests.

标准方法是重试请求几次.如果没有通过,提醒用户检查连接,然后正常失败.

The standard approach is to retry the request a few times. If it doesn't go through, alert the user to check the connection, and fail gracefully.

旁注:将整个应用程序置于离线"状态可能会导致处理状态的许多容易出错的工作......无线连接可能会来来去去等等.所以你最好打赌可能只是优雅地失败,保留数据,并提醒用户......允许他们最终解决连接问题(如果有的话),并继续使用你的应用程序,并有一定的宽恕.

Sidenote: To put the entire application in an "offline" state may lead to a lot of error-prone work of handling state.. wireless connections may come and go, etc. So your best bet may be to just fail gracefully, preserve the data, and alert the user.. allowing them to eventually fix the connection problem if there is one, and to continue using your app with a fair amount of forgiveness.

旁注:您可以检查像谷歌这样的可靠网站的连接性,但这可能并不完全有用,因为只是尝试提出您自己的请求,因为虽然谷歌可能可用,但您自己的应用程序可能不是,您仍然必须处理自己的连接问题.尝试向谷歌发送 ping 是确认互联网连接本身已关闭的好方法,因此如果该信息对您有用,那么麻烦就值得了.

Sidenote: You could check a reliable site like google for connectivity, but this may not be entirely useful as just trying to make your own request, because while Google may be available, your own application may not be, and you're still going to have to handle your own connection problem. Trying to send a ping to google would be a good way to confirm that the internet connection itself is down, so if that information is useful to you, then it might be worth the trouble.

旁注:发送 Ping 的实现方式与您发出任何类型的双向 ajax 请求的方式相同,但发送 ping 到 google,在这种情况下,会带来一些挑战.首先,我们会遇到与进行 Ajax 通信时通常遇到的相同的跨域问题.一种选择是设置服务器端代理,其中我们实际上 ping google(或任何站点),并将 ping 的结果返回给应用程序.这是一个catch-22,因为如果互联网连接确实存在问题,我们将无法访问服务器,如果连接问题仅出现在我们自己的域中,我们将不会无法区分.可以尝试其他跨域技术,例如,在指向 google.com 的页面中嵌入 iframe,然后轮询 iframe 的成功/失败(检查内容等).嵌入图像可能并不能真正告诉我们任何信息,因为我们需要来自通信机制的有用响应,以便对正在发生的事情得出一个好的结论.因此,再次确定整个互联网连接的状态可能比它的价值更麻烦.您必须为您的特定应用权衡这些选项.

Sidenote: Sending a Ping could be achieved in the same way that you would make any kind of two-way ajax request, but sending a ping to google, in this case, would pose some challenges. First, we'd have the same cross-domain issues that are typically encountered in making Ajax communications. One option is to set up a server-side proxy, wherein we actually ping google (or whatever site), and return the results of the ping to the app. This is a catch-22 because if the internet connection is actually the problem, we won't be able to get to the server, and if the connection problem is only on our own domain, we won't be able to tell the difference. Other cross-domain techniques could be tried, for example, embedding an iframe in your page which points to google.com, and then polling the iframe for success/failure (examine the contents, etc). Embedding an image may not really tell us anything, because we need a useful response from the communication mechanism in order to draw a good conclusion about what's going on. So again, determining the state of the internet connection as a whole may be more trouble than it's worth. You'll have to weight these options out for your specific app.

这篇关于检测互联网连接是否离线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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