Don*_*rek 31 networking node.js raspberry-pi
在Raspberry Pi上安装了NodeJS,有没有办法检查rPi是否通过NodeJ连接到互联网?
Jar*_*uba 41
虽然robertklep的解决方案有效,但它远不是最佳选择.dns.resolve超时大约需要3分钟,如果您没有互联网连接则会出错,而dns.lookup几乎可以立即响应错误ENOTFOUND.
所以我做了这个功能:
function checkInternet(cb) {
require('dns').lookup('google.com',function(err) {
if (err && err.code == "ENOTFOUND") {
cb(false);
} else {
cb(true);
}
})
}
// example usage:
checkInternet(function(isConnected) {
if (isConnected) {
// connected to the internet
} else {
// not connected to the internet
}
});
Run Code Online (Sandbox Code Playgroud)
这是迄今为止检查互联网连接的最快方法,它避免了与互联网连接无关的所有错误.
rob*_*lep 31
一种快速而肮脏的方法是检查Node是否可以解决www.google.com:
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No connection");
} else {
console.log("Connected");
}
});
Run Code Online (Sandbox Code Playgroud)
这并非完全万无一失,因为您的RaspPi可以连接到Internet但由于www.google.com某种原因无法解决,您可能还需要检查err.type以区分"无法解析"和"无法连接到名称服务器,因此连接可能落下').
我不得不在不久前在NodeJS-app中构建类似的东西.我这样做的方法是首先使用networkInterfaces()函数是OS模块,然后检查一个或多个接口是否具有非内部IP.
如果这是真的,那么我使用exec()开始ping一个定义明确的服务器(我喜欢谷歌的DNS服务器).通过检查exec()的返回值,我知道ping是否成功.我根据接口类型调整了ping的数量.分叉流程会带来一些开销,但由于此测试在我的应用程序中执行不太频繁,我可以负担得起.此外,通过使用ping和IP地址,您不依赖于配置DNS.这是一个例子:
var exec = require('child_process').exec, child;
child = exec('ping -c 1 128.39.36.96', function(error, stdout, stderr){
if(error !== null)
console.log("Not available")
else
console.log("Available")
});
Run Code Online (Sandbox Code Playgroud)
这是一个单行:(节点 10.6+)
let isConnected = !!await require('dns').promises.resolve('google.com').catch(()=>{});
Run Code Online (Sandbox Code Playgroud)
尽管不是万无一失,但要完成工作:
var dns = require('dns');
dns.lookupService('8.8.8.8', 53, function(err, hostname, service){
console.log(hostname, service);
// google-public-dns-a.google.com domain
});
Run Code Online (Sandbox Code Playgroud)
只需使用简单if(err)并充分处理响应.:)
ps.:请不要打扰告诉我8.8.8.8不是要解决的名称,它只是从谷歌查找高可用性DNS服务器.目的是检查连接性,而不是名称解析.
由于我在这里关心其他解决方案中的 DNS 缓存,因此我尝试使用 http2 进行实际的连接测试。我认为这是测试互联网连接的最佳方法,因为它不会发送太多数据,也不依赖于单独的 DNS 解析,而且速度相当快。
请注意,这是在 v8.4.0 中添加的
const http2 = require('http2');
function isConnected() {
return new Promise((resolve) => {
const client = http2.connect('https://www.google.com');
client.on('connect', () => {
resolve(true);
client.destroy();
});
client.on('error', () => {
resolve(false);
client.destroy();
});
});
};
isConnected().then(console.log);
Run Code Online (Sandbox Code Playgroud)
编辑:如果有人感兴趣的话,我将其制作成一个包。