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

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

问题描述

如何检测,在JavaScript中的互联网连接离线?

How to detect that the Internet connection is offline in JavaScript?

推荐答案

您可以决定通过的的连接丢失失败XHR请求

标准的办法是为重试请求几次。如果没有通过,提醒用户以检查连接,和无法正常

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.

旁注:要放在一个脱机状态,整个应用程序可能会导致大量的处理状态容易出错的工作..无线连接可能会来来去去,等,所以你最好办法可能是只是正常失败,preserve数据,并提醒用户..使他们能够最终解决连接问题如果有,并继续使用与宽恕了相当数量的应用程序。

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.

旁注发送平安的,可以以同样的方式,你会做任何形式的双向Ajax请求实现,但发送一个ping本到谷歌案件会带来一些挑战。首先,我们不得不说,通常遇到让AJAX通信相同的跨域问题。一种选择是建立一个服务器端代理,其中我们实际上谷歌(或其他部位),以及平的结果返回给应用程序。这是一个第二十二条,因为如果网络连接实际上是问题,我们将无法到达服务器,如果连接问题只是我们自己的域名,我们不会可以看出其中的差别。其他跨域技术可以尝试,例如,嵌入在页面中的iframe指向google.com,然后投票的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天全站免登陆