如何使用 HTML5/JavaScript 检查连接类型(WiFi/LAN/WWAN)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11701328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:46:49  来源:igfitidea点击:

How do I check connection type (WiFi/LAN/WWAN) using HTML5/JavaScript?

javascripthtmlwwan

提问by Chris

I wish to tailor a particular website to a low bandwidth version if the client is connected via WWAN (metered) connection or otherwise.

如果客户端通过 WWAN(计量)连接或其他方式连接,我希望将特定网站定制为低带宽版本。

The connection type is important for the site to know if it should provide a rich experience or a bandwidth efficient experience. My desktop runs on a metered connection and in the future, more desktops will be connected via metered cellular networks (including Windows 8 tablet PCs), efficient web-apps should respect that a user might not require high detail assets on a pay-per-byte-connection.

连接类型对于站点了解它是否应该提供丰富的体验或带宽高效的体验很重要。我的桌面在按流量计费的连接上运行,将来,更多的台式机将通过按流量计费的蜂窝网络(包括 Windows 8 平板电脑)连接,高效的网络应用程序应该尊重用户可能不需要按次付费的高细节资产。字节连接。

How do I check if the client is connected via WiFi/LAN/WWAN using HTML5/JavaScript?

如何使用 HTML5/JavaScript 检查客户端是否通过 WiFi/LAN/WWAN 连接?

NOTE : I do not wish to explicitly check for browser headers (which is not really a solution for desktop browsers that can connect to the internet via multiple methods).

注意:我不希望明确检查浏览器标头(对于可以通过多种方法连接到互联网的桌面浏览器来说,这并不是真正的解决方案)。

回答by Rob W

See navigator.connection(prefixed). This API exposes information about the bandwidth of the client.

navigator.connection(前缀)。此 API 公开有关客户端带宽的信息。

回答by Bergi

You can't. The information on how a client is connected (i.e., which technologies it uses, if it is pay-per-byte or a flatrate) to a server is not public. You might be able to get a trace route (with all problems that are connected to tracing), if that helps you, but that information won't be accessible JavaScript.

你不能。关于客户端如何连接到服务器的信息(即,它使用哪些技术,是按字节付费还是按固定收费)是不公开的。如果这对您有帮助,您可能能够获得跟踪路由(包含与跟踪相关的所有问题),但该信息将无法通过 JavaScript 访问。

What can do with JS is simple bandwith testing. Download a file of known size via XHR, and measure the time that needs.

JS 可以做的是简单的带宽测试。通过 XHR 下载已知大小的文件,并测量所需的时间。

The pay model of a connection is absolute private data. Neither will it be sent along to servers on the internet, nor is it available to a loaded website. If you need this information to offer the client a custom app, ask the userdirectly. He will tell you if he wants.

连接的付费模式是绝对私有数据。它既不会被发送到互联网上的服务器,也不会被加载的网站使用。如果您需要此信息来为客户提供自定义应用程序,直接询问用户。如果他愿意,他会告诉你的。



Yet, wait. Latest Chrome and Firefox can be set (user preference) to provide that data on the experimental navigator.connectionobject.

然而,等等。可以设置最新的 Chrome 和 Firefox(用户偏好)以提供有关实验navigator.connection对象的数据。

Also, for developing Metro apps, Windows offeres such information on the Windows.Networking.ConnectivityAPI, see this tutorial.

此外,对于开发 Metro 应用程序,Windows 提供有关Windows.Networking.ConnectivityAPI 的此类信息,请参阅本教程

回答by Gerard Sexton

Maybe you are approaching it in the wrong way. Have a look at the Gmail model. They have a rich client that the user can opt out of if the page is taking a long time to load. The standard client is much lighter and uses a more "traditional" web design.
Trying to automatically detect things that are not meant to be can take you down a very deep rabbit hole.

也许你以错误的方式接近它。看看 Gmail 模型。他们有一个富客户端,如果页面加载时间很长,用户可以选择退出。标准客户端要轻得多,并使用更“传统”的网页设计。
试图自动检测不该出现的东西会让你陷入一个非常深的兔子洞。

回答by Chrispy

Old question, but in searching for my own purposes, I found this -- I have yet to try it and so therefore YMMV: https://github.com/ashanbh/detectClientSpeed.

老问题,但在寻找我自己的目的时,我发现了这个——我还没有尝试过,因此 YMMV:https: //github.com/ashanbh/detectClientSpeed

The underlying notion of measuring time to download an asset to derive bandwidth information is subjective and not necessarily consistent, but short of native platform API for iOS/Android, this is not a bad option. A lighter alternative may be to measure ping times to something like google.com.

测量下载资产的时间以获得带宽信息的基本概念是主观的,不一定一致,但缺乏适用于 iOS/Android 的原生平台 API,这不是一个坏选择。更轻松的替代方法可能是测量 google.com 之类的 ping 时间。