【问题标题】:Events for network status change in Node.js?Node.js 中网络状态更改的事件?
【发布时间】:2019-01-05 19:46:05
【问题描述】:

我发现的所有解决方案都只是轮询服务。例如。他们每秒 ping google.com 以查看 Node 服务是否可以访问 Internet。

但是,我正在寻找一种更简洁的基于事件的解决方案。在浏览器中,有window.ononlinewindow.onoffline。我知道这些并不完美,但它们总比没有好。

我不一定要寻找一种方法来查看 Node 服务是否在线,我只是在寻找一种方法来查看操作系统是否认为它在线。例如。如果操作系统没有连接到任何网络接口,那么它肯定是离线的。但是,如果它连接到路由器,那么我也许可以 ping google.com

【问题讨论】:

  • 没有循环你什么都做不了,nodeJs 是基于事件的,它有一个循环。因此,也许只是循环 ping DNS 而不是 google 以查看您是否已连接到网络,或者依赖您通过套接字与之交谈的其他服务,该服务会告诉您何时未连接到 Internet 或特定网络(可能是基于硬件的解决方案,硬连接到你的 nodeJs 运行机器)
  • 检查此链接,这可能会有所帮助stackoverflow.com/questions/15270902/…

标签: javascript node.js network-programming


【解决方案1】:

我相信目前,这是判断您是否已连接的最有效方式。

如果您想要一个“基于事件”的解决方案,您可以使用以下内容包装轮询服务:

connectivity-checker.js

const isOnline = require("is-online");

function ConnectivityChecker (callback, interval) {
    this.status = false;
    this.callback = callback;
    this.interval = this.interval;
    // Determines if the check should check on the next interval
    this.shouldCheckOnNextInterval = true;
}

ConnectivityChecker.prototype.init = function () {
    this.cleanUp();

    this.timer = setInterval(function () {
        if (this.shouldCheck) {
            isOnline().then(function (status) {
                if (this.status !== status) {
                    this.status = status;
                    this.callback(status);
                    this.shouldCheckOnNextInterval = true;
                }
            }).catch(err => {
                console.error(err);
                this.shouldCheckOnNextInterval = true;
            })

            // Disable 'shouldCheckOnNextInterval' if current check has not resolved within the interval time
            this.shouldCheckOnNextInterval = false;
        }
    }, this.interval);
}

ConnectivityChecker.prototype.cleanUp = function () {
    if (this.timer) clearInterval(this.timer);
}

export { ConnectivityChecker };

然后在您的使用站点中(例如app.js

app.js

const { ConnectivityChecker } = require("/path/to/connectivity-checker.js");

const checker = new ConnectivityChecker(function(isOnline) {
    // Will be called ONLY IF isOnline changes from 'false' to 'true' or from 'true' to 'false'.
    // Will be not called anytime isOnline status remains the same from between each check
    // This simulates the event-based nature you're looking for


    if (isOnline) {
        // Do stuff if online
    } else  {
        // Do stuff if offline
    }
}, 5000);

// Where your app starts, call
checker.init();

// Where your app ends, call
// checker.cleanUp();

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    您的假设部分正确。从操作系统级别,您可以检测您是否连接到网络,但这并不能保证您可以访问互联网。此外,在本地检查我是否有网络连接对于不同的操作系统会有所不同。

    因此,了解您是否有 Internet 连接的最可靠方法是尝试从 Internet 访问任何资源,看看您是否成功。由于某些原因,该资源可能无法访问,因此您的策略应该是访问多个知名资源并查看您是否可以访问其中任何一个。

    为此,我使用了is-online npm 包。它依赖于访问互联网资源和 DNS 解析来检查您是否连接到互联网。

    示例代码:

    const isOnline = require('is-online');
    
    isOnline().then(online => {
      if(online){
         console.log("Connected to internet");
      }else{
         console.log("Not connected to internet");
      }
     });
    

    【讨论】:

      【解决方案3】:

      ** 更新

      我认为这个模块有你正在寻找的东西,它有许多与网络接口一起使用的有用方法。

      https://www.npmjs.com/package/network

      基本示例:告诉您是否连接到网关,并返回其 IP。

      *(我在我的机器上测试过)

      var network = require("network");
      
      network.get_gateway_ip(function(err, ip) {
        if (err) {
          console.log("No Gateway IP found!");
        } else {
          console.log(`Gateway IP: ${ip}`);
        }
      });
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多