Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//#!/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
You can’t perform that action at this time.