简体   繁体   English

Cordova插件可检测互联网连接

[英]Cordova plugin to detect internet connection

my cordova app requires internet connection for some actions. 我的cordova应用程序需要互联网连接才能执行某些操作。 i am using https://github.com/apache/cordova-plugin-network-information to get an event whenever the app goes online or offline. 每当应用程序在线或离线时,我都会使用https://github.com/apache/cordova-plugin-network-information来获取事件。 It works well with Mobile Data, but for Wifi, it acts a bit strange. 它可以很好地与Mobile Data配合使用,但是对于Wifi而言,它的行为有点奇怪。 Whether my wifi is connected to the internet or not, it reports that i am online in both cases. 无论我的wifi是否连接到Internet,在两种情况下它都报告我在线。

The behavior that i am expecting is to say i am online only when the wifi is connected to the internet. 我期望的行为是仅在WiFi连接到Internet时才说我在线。 i am running ionic framework, angularjs, cordova 3.5.1, and using an Android, Galaxy S3 for testing. 我正在运行离子框架,angularjs,cordova 3.5.1,并使用Android,Galaxy S3进行测试。

The plugin does not detect if the user has access to the Internet, but only if the user is on a network. 该插件不会检测用户是否可以访问Internet,而只会检测到用户在网络上。 Since your phone is on a WiFi network even though the WiFi network doesn't have access to the Internet, the plugin is performing as designed. 由于即使WiFi网络无法访问Internet,您的电话也位于WiFi网络上,因此插件的运行情况与设计相符。

In order to test connectivity, you must always attempt the request -- this is the only way to truly know if the device has Internet access. 为了测试连接性,您必须始终尝试请求-这是真正知道设备是否可以访问Internet的唯一方法。 So if you're trying to reach www.google.com , you have to attempt the request and handle any errors that result. 因此,如果您尝试访问www.google.com ,则必须尝试请求并处理所有导致的错误。 The only benefit to having the network plugin in place is that you have a little more information about whether or not it is even worth attempting the request in the first place -- if the device is not connected to any network, you know there is little benefit in trying the request. 放置网络插件的唯一好处是,您可以获得关于是否值得一开始尝试该请求的更多信息-如果设备未连接到任何网络,则说明几乎没有尝试请求的好处。

If there is no route, your request should fail nearly immediately. 如果没有路线,您的请求应该几乎立即失败。 If there is some other issue, your request may time out instead. 如果还有其他问题,您的请求可能会超时。 Be sure to indicate some progress to the user in the case of a long request, since time outs can take several seconds (or minutes) to occur. 在超时的情况下,请务必向用户指示进度,因为超时可能要花费几秒钟(或几分钟)。

If your app needs to target specific hosts, those would be the same hosts you'd use to verify connectivity. 如果您的应用需要定位到特定主机,则这些主机将与您用来验证连接性的主机相同。 If your app targets generic hosts (or user-entered hosts), test with the specific host to test connectivity. 如果您的应用以通用主机(或用户输入的主机)为目标,请测试特定主机以测试连接性。

  • Don't assume that because a device can reach one host that it means it can reach another, either -- some networks may block certain hosts while allowing access to others. 不要以为一台设备可以到达一台主机就意味着它也可以到达另一台主机-某些网络可能会阻止某些主机同时允许访问其他主机。
  • Don't test with a host that isn't part of the actual request, especially if it is one you don't control. 不要使用不属于实际请求的主机进行测试,尤其是如果您无法控制的主机。 That is, if you want to test if the user can reach www.example.com , don't ping www.google.com . 也就是说,如果您要测试用户是否可以访问www.example.com ,请不要ping www.google.com Your attempt might be taken as a malicious attack, especially if your app became popular. 您的尝试可能会被视为恶意攻击,尤其是在您的应用流行后。

One other tip: 另一个提示:

DO check the network type to see if you need to limit data (in the case of cellular networks) but DON'T assume the network type can be used to indicate data transfer speeds -- just because you're on a 4G network does not mean that you will be guaranteed fast data transfer. 请检查网络类型以查看是否需要限制数据(在蜂窝网络中),但不要假设该网络类型可用于指示数据传输速度-仅仅因为您使用的是4G网络意味着您将获得快速的数据传输。 Of course, the same applies to WiFi. 当然,同样适用于WiFi。

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

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