javascript - 为什么 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
价值。
环境:
- macOS 10.15.7
- 微软边缘 89.0.774.63
解决方案
如果您查看W3C API 规范中的effectiveType
定义NetworkInformation
,wifi
它不是“有效连接类型”之一。EffectiveConnectionType
枚举4g
用作快速和可靠连接的最高名称。
type
on 属性navigator.connection
使用ConnectionType
枚举作为它的值,wifi
它的可能值之一也是如此。但是,只有 Chrome OS 上的 Chrome 支持APItype
中的字段NetworkInformation
,所以 Edge 总是会说type
is undefined
。
如果您在生产中基于此技术推出一项功能,我会倾向于使用effectiveType
,但该NetworkInformation
API 仍然是一项实验性技术,处于非官方草案中,并且在浏览器中没有得到很好的支持。
推荐阅读
- python - 用 Pandas 扁平化父子关系
- sql - 我必须提取诸如大多数员工是哪一年加入公司的信息?显示员工的年份和人数
- c# - .Net Core(EF Core)中多对多关系的最佳解决方案是什么?
- if-statement - Google Apps Scripts If 函数不跳过空值的行
- ruby-on-rails - 嵌套参数不适用于 Pundit
- sql-server - 设置单用户模式后死锁
- regex - NGX MASK 选项的字母数字出现错误的位置
- android - 在 Parse Android 中获取 List 的大小为零
- azure - 如何将我的 Azure 机器学习工作区导出/移动到不同的订阅?
- android - 无法在 Linux 上使用命令行工具运行 android 模拟器(恐慌:损坏的 AVD 系统路径。)