Created
August 11, 2016 09:40
-
-
Save dazld/f0e2385169031a6bd7a93c5b77da7729 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dns = require('dns'); | |
console.log(dns.getServers()); | |
function getAddress(uri) { | |
return new Promise(function(res, rej) { | |
dns.resolve4(uri, function(err, address) { | |
if (err) { | |
rej(err); | |
} | |
res(address); | |
}); | |
}); | |
} | |
function makeResolver(uri) { | |
let timeout; | |
let lastResolvedAddress; | |
function pingAndQueue () { | |
getAddress(uri).then(function(addresses) { | |
const addr = addresses.pop(); // multiple addresses? | |
if (lastResolvedAddress !== addr) { | |
console.log('ip changed:', addr); | |
lastResolvedAddress = addr; | |
} else { | |
console.log('-'); | |
} | |
timeout = setTimeout(pingAndQueue, 9000); | |
}).catch(function(err) { | |
clearTimeout(timeout); | |
console.log('errr', err); | |
}); | |
} | |
pingAndQueue(); | |
} | |
makeResolver('bip.local'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment