Skip to content

Instantly share code, notes, and snippets.

@cho45
Created July 7, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cho45/19be5dce8afa34c9db2c2858e22486b8 to your computer and use it in GitHub Desktop.
Save cho45/19be5dce8afa34c9db2c2858e22486b8 to your computer and use it in GitHub Desktop.
//#!/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