Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs检测因特网是否断开方案 #27

Open
SugarTeam opened this issue Jun 16, 2020 · 0 comments
Open

nodejs检测因特网是否断开方案 #27

SugarTeam opened this issue Jun 16, 2020 · 0 comments
Labels

Comments

@SugarTeam
Copy link
Contributor

背景

最近在用Electron开发一款应用,其中有涉及到检测因特网是否断开的需求。Electron基于Chromium和Node.js,让你可以使用HTML、CSS和JavaScript构建应用。所以Electron提供nodejs、浏览器两套运行环境。

笔者最先考虑到的方案HTML5提供的online/offline网络连接事件。

window.addEventListener('online', ...)
window.addEventListener('offline', ...)

结论很失望,这两个网络连接事件,只是检测本地网络连接状态。

既然浏览器没有提供检测因特网是否断开的接口,笔者只能在nodejs寻求答案。

调研

说到nodejs,笔者最先想到是去npm仓库搜索现有的库。找到了两款检测因特网状态的库internet-availableis-online

internet-available

这个库检测因特网连接状态原理,是检测dns连接状态。

这里大家肯定有个疑问,使用nodejs原生模块dns不是更简洁吗?

你说的没错,nodejs确实提供这样的方法,但是dns原生模块并没有提供超时检测。internet-available可以设置超时参数,默认是5000ms(依赖dns-socket库实现dns超时,有兴趣可以研究其源码,这里不做展开)。

internet-available使用举例:

var internetAvailable = require("internet-available");

internetAvailable().then(function(){
    console.log("Internet available");
}).catch(function(){
    console.log("No internet");
});

如果想加入检测次数和每次检测超时时间,代码如下:

var internetAvailable = require("internet-available");

internetAvailable({
    timeout: 4000,
    retries: 10,
}).then(function(){
    console.log("Internet available");
}).catch(function(){
    console.log("No internet");
});

internet-available默认检测的DNS域名是google.com,如果想自定义域名,代码如下:

var internetAvailable = require("internet-available");

internetAvailable({
    domainName: "xxxxx.com",
    port: 53,
    host: '8.8.8.8' // 默认,国内请改成114.114.114.114
}).then(() => {
    console.log("Internet available");
}).catch(() => {
    console.log("No internet");
});

备注:8.8.8.8是谷歌公司提供的免费DNS服务器,该地址是全球通用,相对来说,更适合国外以及访问国外网站的用户使用,国内更适合用114.114.114.114。

is-online

is-online与internet-available检测方式相同,唯一区别是is-online可以在nodejs和浏览器环境同时运行。在浏览器环境下,通过navigator.onLine返回网络连接状态,但与HTML5 online、offline事件一样,只能检测本地连接。

is-online使用举例

const isOnline = require('is-online');
 
isOnline().then(online => {
    if(online){
        console.log("We have internet");
    }else{
        console.log("Houston we have a problem");
    }
});

此库也提供超时设置,且可以设置Internet协议版本,这是一个通常不需要设置的高级选项,但它对于专门断言IPv6连接非常有用,代码如下:

var isOnline = require('is-online');
 
isOnline({
    timeout: 5000,
    version: "v4" // v4 or v6
}).then(online => {
    if(online){
        console.log("Internet available");
    }else{
        console.log("No internet");
    }
});

总结

除了以上两个库,还有其他方式可以检测因特网断开吗?笔者目前想到的还可以通过发起http head请求是否成功响应判断;通过ping 目标host或者domain是否连通检测判断。如果有更好的方式,欢迎讨论~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant