【问题标题】:Node.js - Recreating navigator.onLineNode.js - 重新创建 navigator.onLine
【发布时间】:2018-07-10 11:39:56
【问题描述】:

我有一个 Node.js 应用程序。我正在尝试编写一个实用程序来进行一些网络检测。我创建这个应用程序只是为了了解有关 Node 的更多信息。

在这个应用程序中,我想查看 a) 我的计算机是否在线以及 b) 它是否可以访问互联网。如果我在浏览器中执行此操作,我可以使用navigator.onLine,然后尝试访问一个网站。但是,这不是 Node 中的选项。

我查看了OS module's networkInterfaces method,但是,它似乎没有我需要的东西。例如,如果您去星巴克,您可以“在线”,但在您同意其网络的服务条款之前无法真正访问互联网。我一直在通过在我的机器上切换飞行模式来测试这种情况。无论飞行模式是打开还是关闭,networkInterfaces 方法都会返回相同的内容。

如何在 Node 中重新创建 navigator.onLine 功能?

【问题讨论】:

  • 可以像fetch 到一个 URL 然后检查状态代码这样简单的事情吗?如果您需要进行身份验证(在星巴克的示例中),您应该无法获得200,因为您实际上不会连接到该页面。看起来很老套,但它可能会节省一些时间。
  • 这看起来像是在 NodeJs 中检查互联网连接的副本 - stackoverflow.com/questions/15270902/…
  • 看看repo:is-online

标签: javascript node.js


【解决方案1】:

这是检查互联网是否可用的代码。

为此安装is-online 模块:npm i is-online

index.js:

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

isOnline({
    timeout: 1000
}).then(online =>{
    if(online)
        console.log('Internet is available')
    else
        console.log('Internet is not available')
})

【讨论】:

    【解决方案2】:

    通过调用onConnected并传递建立连接时将调用的回调函数

    const dns = require('dns');
    
    function onConnected(callback){
        dns.resolve('www.google.com', function(err) {
            if (!err) callback();
        });
    }
    

    【讨论】:

      【解决方案3】:

      您可以全局定义navigator 变量并将onLine 参数设置为真或假。

      const dns = require('dns');
      const navigator = global.navigator = {onLine: false};
      
      ((navigator) => {
        const dns = require('dns');
        setInterval(() => {
          dns.resolve('www.google.com', 
                      error => navigator.onLine = !error);
        }, 1000);
      })(navigator);
      
      
      setInterval(() => {
        console.log('Is online:', navigator.onLine ? 'yes' : 'no');
      }, 1500);
      

      【讨论】:

        【解决方案4】:

        我会走这条路

        var exec = require('child_process').exec, child;
        child = exec('ping -c 1 8.8.8.8', function(error, stdout, stderr){
             if(error !== null)
                  console.log("Not available")
              else
                  console.log("Available")
        });
        

        或尝试 dns 查找

        require('dns').lookupService('8.8.8.8', 53, function(err, hostname, service){
          console.log(hostname, service);
            // google-public-dns-a.google.com domain
        });
        

        可能正在尝试解析域名

        function checkNameResolution(cb) {
            require('dns').lookup('google.com',function(err) {
                if (err && err.code == "ENOTFOUND") {
                    cb(false);
                } else {
                    cb(true);
                }
            })
        }
        

        真的取决于您的用例。 就我个人而言,我会为一个高度可用的地址的 dnslookup 服务。但是,如果您在某种解析域的 dns 掩码背后做 localhost 它可能无效,这反过来又会增加/代替测试名称解析的需要。将它们组合到像 checkInternetConnection() 这样的函数中,您将尽其所能。

        【讨论】:

          猜你喜欢
          • 2015-02-27
          • 1970-01-01
          • 2014-02-08
          • 2020-01-04
          • 1970-01-01
          • 2019-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多