首页 > 解决方案 > 为什么 NetworkInformation.effectiveType 给我返回的是 4g 网络而不是 wifi?

问题描述

我正在使用网络信息 API来获取网络类型。

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;

// output in Microsoft edge browser console
// {onchange: null, effectiveType: "4g", rtt: 100, downlink: 1.5, saveData: false}

我正在使用公司的 wifi 网络。我预计effectiveType财产的价值应该是wifi。因为我有一些这样的代码逻辑:

if(effectiveType === 'wifi') {
  // Do something
} else if (effectiveType === '4g') {
  // Do other things 
}

那可能吗?我找到了NetworkInformation.type只读属性,但它给了我undefined. 它不支持边缘。

我看到EffectiveConnectionType 枚举没有wifi价值。

环境:

标签: javascriptnetworkinformation

解决方案


如果您查看W3C API 规范中的effectiveType定义NetworkInformationwifi它不是“有效连接类型”之一。EffectiveConnectionType枚举4g用作快速和可靠连接的最高名称。

typeon 属性navigator.connection使用ConnectionType枚举作为它的值,wifi它的可能值之一也是如此。但是,只有 Chrome OS 上的 Chrome 支持APItype中的字段NetworkInformation,所以 Edge 总是会说typeis undefined

如果您在生产中基于此技术推出一项功能,我会倾向于使用effectiveType,但该NetworkInformationAPI 仍然是一项实验性技术,处于非官方草案中,并且在浏览器中没有得到很好的支持


推荐阅读