简体   繁体   English

navigator.onLine的Internet连接状态无法正常工作,并且在未连接Internet时显示错误状态

[英]Internet connectivity status by navigator.onLine is not working correctly, and showing the wrong status while internet is not connected

I'm creating a web-based application which can work offline as well as online. 我正在创建一个基于Web的应用程序,它既可以脱机也可以在线运行。 Therefor, I have need to check if internet is working properly or not. 因此,我需要检查互联网是否正常工作。 Currently I'm using 目前我正在使用

navigator.onLine 导航器

in JavaScript. 在JavaScript中。 I tried something like; 我尝试过类似的东西;

if (navigator.onLine) {
    // Internet is connected
    // work in online Mode
}
else{
    // Internet is disconnected
    // work in offline mode
}

Its work fine, but sometimes it gives wrong status. 它的工作正常,但有时会给出错误的状态。 I noticed it when I change my ip and/or gateway and set them wrong in order to disconnect my internet access. 当我更改IP和/或网关并将它们设置为错误以断开Internet访问时,我注意到了这一点。 No any web-page was being accessed after change my ip but navigator.onLine showing the internet status online. 更改我的IP后,没有任何网页可以访问,但是navigator.onLine显示了在线Internet状态。 I restarted my system, clear browser cache, but I faced the same issue. 我重新启动系统,清除了浏览器缓存,但遇到了同样的问题。 I also read about the same issue people were facing, but didn't get a good solution. 我还阅读了有关人们面临的同一问题,但没有得到好的解决方案。 How can I solve this issue! 我该如何解决这个问题! Any Suggestions! 有什么建议么!

As mentioned in documentation for this feature: 如该功能的文档中所述:

In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; 在Chrome和Safari中,如果浏览器无法连接到局域网(LAN)或路由器,则表明该浏览器处于脱机状态; all other conditions return true. 所有其他条件都返回true。 So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. 因此,尽管您可以假定浏览器在返回错误值时处于脱机状态,但不能假设真实值必然意味着浏览器可以访问Internet。 You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." 您可能会得到误报,例如,当计算机运行的虚拟化软件具有始终“连接”的虚拟以太网适配器时。 Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking... 因此,如果您真的想确定浏览器的在线状态,则应该开发其他方法来检查...

https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine https://developer.mozilla.org/zh-CN/docs/Web/API/NavigatorOnLine/onLine

Meaning that you cannot rely on this feature to determine the connectivity. 这意味着您不能依靠此功能来确定连接性。 Now, it depends on your application - what you can do. 现在,这取决于您的应用程序-您可以做什么。 One way is to have a heart-beat packets sent to some REST interface on your server. 一种方法是将心跳数据包发送到服务器上的某些REST接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM