• Scotland team
  • Author: Tomey

background

Electron is currently developing an application that involves the need to detect whether the Internet is disconnected. Electron is based on Chromium and Node.js and lets you build applications using HTML, CSS, and JavaScript. Therefore, Electron provides both NodeJS and browser running environment.

The first scheme I consider is the online/ Offline network connection event provided by HTML5.

window.addEventListener('online', ...)
window.addEventListener('offline',...).Copy the code

Conclusion Disappointed, these two network connection events only detect local network connection status.

Since the browser does not provide an interface to detect whether the Internet is down, I can only find the answer in NodeJS.

research

When it comes to NodeJS, my first thought is to search the NPM repository for existing libraries. Two libraries of Internet -available and IS – Online are found

internet-available

This library detects the state of Internet connections by checking the state of DNS connections.

The nodeJS native DNS module is much simpler to use.

You are right, NodeJS does provide such a method, but the DNS native module does not provide timeout detection. Internet -available You can set the timeout parameter. The default value is 5000ms.

Internet-available Example:

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

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

If you want to add the detection times and the timeout time of each detection, the code is as follows:

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

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

Internet -available The default DNS domain name is google.com. If you want to customize the domain name, the code is as follows:

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

internetAvailable({
    domainName: "xxxxx.com",
    port: 53,
    host: '8.8.8.8'Then (() => {console.log())"Internet available");
}).catch(() => {
    console.log("No internet");
});
Copy the code

Note: 8.8.8.8 is the free DNS server provided by Google. This address is universal. Relatively speaking, it is more suitable for foreign countries and users visiting foreign websites.

is-online

Is-is Online is the same as Internet -available detection. The only difference is that IS-IS Online can run in nodeJS and browser environment at the same time. In the browser environment, the network connection status is returned through navigator.onLine, but like HTML5 onLine and Offline events, only local connection can be detected.

Example for using IS-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"); }});Copy the code

The library also provides timeout Settings and the ability to set Internet protocol versions, an advanced option that does not normally need to be set, but is useful for specifically asserting IPv6 connections, as follows:

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"); }});Copy the code

conclusion

Besides the above two libraries, is there any other way to detect Internet disconnection? The author can also be judged by launching the HTTP HEAD request whether the response is successful; Check whether the target host or domain is connected by ping. If there is a better way, welcome to discuss ~