diff --git a/io/mdns/README.md b/io/mdns/README.md index d09377d8..d4b944d5 100644 --- a/io/mdns/README.md +++ b/io/mdns/README.md @@ -4,6 +4,8 @@ node-red-node-discover A Node-RED node that uses Bonjour / Avahi to discover local network services such as iTunes libraries, printers, etc. +Now also supports announcing new services. + Prerequisites ------------- @@ -11,19 +13,21 @@ please read the [install instructions](https://www.npmjs.com/package/mdns) for t For Debian / Ubuntu this requires installing - sudo apt-get install libavahi-compat-libdnssd-dev + sudo apt-get install libavahi-compat-libdnssd-dev Install ------- -Run the following command in the root directory of your Node-RED install +Run the following command in the root directory of your Node-RED install, usually `~/.node-red` - npm install node-red-node-discovery + npm install node-red-node-discovery Usage ----- -Uses a javascript implenetation of mdns ( mdns-js ) to provide a Bonjour / Avahi +### Discovery + +Uses an implemetation of mdns to provide a Bonjour / Avahi service discovery capability. **msg.payload** contains the service object on both arrival and leaving. @@ -40,4 +44,19 @@ Within the msg.payload object the most interesting things are: For a full list of official service types see [this list](http://www.dns-sd.org/ServiceTypes.html" target="_new). -**Note**: When Node-RED starts you will get a big WARNING message about the Bonjour compatability layer... this is just a warning so don't worry. +###Announce + +Provides a Bonjour / Avahi / Zeroconf announcement node. + +If **msg.payload** is 0 - the announcement is stopped. Any other value starts the announcement process. + +The announcement can be customised by the msg if not configured in the edit panel. + + - **msg.service** - For a full list of official service types see this list. + - **msg.port** - the tcp or udp port to use. + - **msg.name** - the short description name of the service. If you use %h in the name, it will be replaced by the machine hostname. + - **msg.txtRecord** - a set of comma separated name:value pairs + +###Note: + +When Node-RED starts you will get a big WARNING message about the Bonjour compatability layer... this is just a warning so don't worry. diff --git a/io/mdns/mdns.html b/io/mdns/mdns.html index 579d922b..17e4be39 100644 --- a/io/mdns/mdns.html +++ b/io/mdns/mdns.html @@ -16,15 +16,15 @@ + + + + + + diff --git a/io/mdns/mdns.js b/io/mdns/mdns.js index e2964287..966af28f 100644 --- a/io/mdns/mdns.js +++ b/io/mdns/mdns.js @@ -28,24 +28,69 @@ module.exports = function(RED) { var browser = mdns.createBrowser(this.service); var node = this; - browser.on('serviceUp', function (service) { + browser.on('serviceUp', function(service) { if (RED.settings.verbose) { node.log("here : " + service.name); } service.state = true; - var msg = {topic: node.topic, payload: service}; + var msg = {topic:node.topic, payload:service}; node.send(msg); }); - browser.on('serviceDown', function (service) { + browser.on('serviceDown', function(service) { if (RED.settings.verbose) { node.log("away : " + service.name); } service.state = false; - var msg = {topic: node.topic, payload: service}; + var msg = {topic:node.topic, payload:service}; node.send(msg); }); browser.start(); - node.on("close", function () { + node.on("close", function() { if (browser) { browser.stop(); } }); } - RED.nodes.registerType("discovery", MdnsNode); + + + 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+'}'); } + catch (e) { delete this.txt; }; + } + 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()); + } + if (node.txt || msg.txtRecord) { options.txtRecord = node.txt || msg.txtRecord }; + 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); } diff --git a/io/mdns/package.json b/io/mdns/package.json index 46cfcda1..e85840a0 100644 --- a/io/mdns/package.json +++ b/io/mdns/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-discovery", - "version" : "0.0.6", + "version" : "0.0.7", "description" : "A Node-RED node that uses Bonjour / Avahi to discover nearby services.", "dependencies" : { "mdns" : "2.2.*" @@ -9,7 +9,7 @@ "type":"git", "url":"https://github.com/node-red/node-red-nodes/tree/master/io/mdns" }, - "license": "Apache", + "license": "Apache v2", "keywords": [ "node-red", "mdns", "avahi", "bonjour" ], "node-red" : { "nodes" : {