Created
July 7, 2018 10:46
-
-
Save cho45/19be5dce8afa34c9db2c2858e22486b8 to your computer and use it in GitHub Desktop.
This file contains 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
//#!/usr/bin/env node | |
const mdns = require('mdns'); | |
function getServiceWithBrowser(browser, timeout) { | |
if (!timeout) timeout = 500; | |
return new Promise(function (resolve, reject) { | |
const ret = []; | |
browser.on('serviceUp', function(service) { | |
ret.push(service); | |
}); | |
//browser.on('serviceDown', function(service) { | |
// console.log("service down: ", service); | |
//}); | |
browser.start(); | |
setTimeout( () => { | |
browser.stop(); | |
resolve(ret); | |
}, timeout); | |
}); | |
} | |
function getAllServices(timeout) { | |
return getServiceWithBrowser(mdns.browseThemAll(), timeout); | |
} | |
function getServiceForType(type, timeout) { | |
return getServiceWithBrowser(mdns.createBrowser(type), timeout); | |
} | |
async function run () { | |
const services = await getAllServices(); | |
const results = await Promise.all(services.map( (s) => getServiceForType(s.type))); | |
const hosts = results.reduce( | |
(r, i) => ( | |
i.forEach((i) => r.set(i.host, i.addresses)), | |
r | |
), | |
new Map() | |
); | |
for (let [name, addresses] of hosts.entries()) { | |
console.log(name); | |
for (let address of addresses) { | |
console.log("\t", address); | |
} | |
} | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment