2015-03-04 20:55:53 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function MdnsNode(n) {
|
|
|
|
var mdns = require('mdns');
|
|
|
|
if (process.platform === "linux") {
|
|
|
|
RED.log.info("You may ignore the warning about Bonjour compatability.");
|
|
|
|
}
|
|
|
|
RED.nodes.createNode(this, n);
|
|
|
|
this.topic = n.topic || "";
|
|
|
|
this.service = n.service;
|
2017-04-12 21:33:55 +02:00
|
|
|
var node = this;
|
2016-01-01 15:31:55 +01:00
|
|
|
// var sequence = [
|
|
|
|
// mdns.rst.DNSServiceResolve(),
|
|
|
|
// mdns.rst.getaddrinfo({families: [4] })
|
|
|
|
// ];
|
|
|
|
// var browser = mdns.createBrowser(this.service,{resolverSequence: sequence});
|
2017-04-12 21:33:55 +02:00
|
|
|
var sequence = [
|
|
|
|
mdns.rst.DNSServiceResolve(),
|
|
|
|
'DNSServiceGetAddrInfo' in mdns.dns_sd ? mdns.rst.DNSServiceGetAddrInfo() : mdns.rst.getaddrinfo({families:[4]}),
|
|
|
|
mdns.rst.makeAddressesUnique()
|
|
|
|
];
|
|
|
|
var browser = mdns.createBrowser((this.service), {resolverSequence:sequence});
|
2015-03-04 20:55:53 +01:00
|
|
|
|
2015-05-08 21:57:15 +02:00
|
|
|
browser.on('serviceUp', function(service) {
|
2015-03-04 20:55:53 +01:00
|
|
|
if (RED.settings.verbose) { node.log("here : " + service.name); }
|
|
|
|
service.state = true;
|
2015-05-08 21:57:15 +02:00
|
|
|
var msg = {topic:node.topic, payload:service};
|
2015-03-04 20:55:53 +01:00
|
|
|
node.send(msg);
|
|
|
|
});
|
2015-05-08 21:57:15 +02:00
|
|
|
browser.on('serviceDown', function(service) {
|
2015-03-04 20:55:53 +01:00
|
|
|
if (RED.settings.verbose) { node.log("away : " + service.name); }
|
|
|
|
service.state = false;
|
2015-05-08 21:57:15 +02:00
|
|
|
var msg = {topic:node.topic, payload:service};
|
2015-03-04 20:55:53 +01:00
|
|
|
node.send(msg);
|
|
|
|
});
|
2017-05-31 23:02:41 +02:00
|
|
|
browser.on('error', function(exception) {
|
|
|
|
node.error(exception.toString());
|
|
|
|
});
|
2015-03-04 20:55:53 +01:00
|
|
|
browser.start();
|
|
|
|
|
2015-05-08 21:57:15 +02:00
|
|
|
node.on("close", function() {
|
2015-04-10 12:56:17 +02:00
|
|
|
if (browser) { browser.stop(); }
|
2015-03-04 20:55:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("discovery", MdnsNode);
|
2015-05-08 21:57:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
function MdnsAnnNode(n) {
|
|
|
|
var mdns = require('mdns');
|
|
|
|
var os = require("os");
|
|
|
|
if (process.platform === "linux") {
|
|
|
|
RED.log.info("You may ignore the warning about Bonjour compatability.");
|
|
|
|
}
|
|
|
|
RED.nodes.createNode(this, n);
|
|
|
|
this.service = n.service || "";
|
|
|
|
this.port = n.port;
|
|
|
|
this.name = n.name;
|
|
|
|
this.txt = n.txt;
|
|
|
|
if (this.txt && (this.txt !== '')) {
|
|
|
|
try { this.txt = JSON.parse('{'+this.txt+'}'); }
|
2015-05-09 00:03:51 +02:00
|
|
|
catch (e) { delete this.txt; }
|
2015-05-08 21:57:15 +02:00
|
|
|
}
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
this.on("input", function(msg) {
|
|
|
|
if ((msg.payload === 0) || (msg.payload === "0")) {
|
|
|
|
node.ad.stop();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var service = node.service || msg.service;
|
|
|
|
var port = Number(node.port || msg.port);
|
|
|
|
var options = {};
|
|
|
|
if (node.name || msg.name) {
|
|
|
|
options.name = (node.name || msg.name).replace(/\%h/g, os.hostname());
|
|
|
|
}
|
2015-05-09 00:03:51 +02:00
|
|
|
if (node.txt || msg.txtRecord) { options.txtRecord = node.txt || msg.txtRecord }
|
2015-05-08 21:57:15 +02:00
|
|
|
node.ad = mdns.createAdvertisement(service, port, options);
|
|
|
|
node.ad.start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("error", function(e) {
|
|
|
|
node.error(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("close", function() {
|
|
|
|
if (node.ad) { node.ad.stop(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("announce", MdnsAnnNode);
|
2015-03-04 20:55:53 +01:00
|
|
|
}
|