A quick and dirty way is to check if Node can resolve www.google.com
:
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No connection");
} else {
console.log("Connected");
}
});
This isn't entire foolproof, since your RaspPi can be connected to the Internet yet unable to resolve www.google.com
for some reason, and you might also want to check err.type
to distinguish between 'unable to resolve' and 'cannot connect to a nameserver so the connection might be down').
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…