1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Add basic announce capability to mdns node.

This commit is contained in:
dceejay 2015-05-08 20:57:15 +01:00
parent 68e639c98a
commit 5dbf104048
4 changed files with 135 additions and 16 deletions

View File

@ -4,6 +4,8 @@ node-red-node-discover
A <a href="http://nodered.org" target="_new">Node-RED</a> 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 <a href="http://www.dns-sd.org/ServiceTypes.html" target="_new">this list</a>.
- **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.

View File

@ -16,15 +16,15 @@
<script type="text/x-red" data-template-name="discovery">
<div class="form-row">
<label for="node-input-service"><i class="icon-gear"></i> Service</label>
<label for="node-input-service"><i class="fa fa-gear"></i> Service</label>
<input type="text" id="node-input-service" placeholder="_http._tcp">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i> Topic</label>
<label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="Topic">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">The service <i>must</i> start with an underscore _ and end ._tcp or ._udp</div>
@ -63,3 +63,58 @@
}
});
</script>
<script type="text/x-red" data-template-name="announce">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-gear"></i> Service</label>
<input type="text" id="node-input-service" placeholder="_http._tcp">
</div>
<div class="form-row">
<label for="node-input-port"><i class="fa fa-random"></i> Port</label>
<input type="text" id="node-input-port" placeholder="9999">
</div>
<div class="form-row">
<label for="node-input-txt"><i class="fa fa-tasks"></i> TxtRecord</label>
<input type="text" id="node-input-txt" placeholder='"name":"value","name2":"value2"'>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name of service">
</div>
<div class="form-tips">%h in the name will be replaced by the machine hostname.</div>
</script>
<script type="text/x-red" data-help-name="announce">
<p>Provides a Bonjour / Avahi / Zeroconf announcement node.</p>
<p>If <b>msg.payload</b> is 0 - the announcement is stopped. Any other value starts the announcement process.</p>
<p>The announcement can be customised by the msg if not configured in the edit panel.</p>
<p><ul>
<li><b>msg.service</b> - For a full list of official service types see <i><a href="http://www.dns-sd.org/ServiceTypes.html" target="_new">this list</a></i>.</li>
<li><b>msg.port</b> - the tcp or udp port to use.</li>
<li><b>msg.name</b> - the short description name of the service. If you use %h in the name, it will be replaced by the machine hostname.</li>
<li><b>msg.txtRecord</b> - a set of comma separated name:value pairs </li>
</ul></p>
</script>
<script type="text/javascript">
RED.nodes.registerType('announce',{
category: 'network-output',
color:"palegoldenrod",
defaults: {
name: {value:""},
service: {value:"",required:true,validate:RED.validators.regex(/^_.*\._(tc|ud)p$/)},
port: {value:""},
txt: {value:""}
},
inputs:1,
outputs:0,
icon: "bonjour.png",
align:"right",
label: function() {
return this.name||this.service||"announce";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -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);
}

View File

@ -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" : {