Partial fix for Issue #45 - allows msg.destip and msg.port properties to override static config.

Signed-off-by: Dave C-J <dave@conway-jones.co.uk>
This commit is contained in:
Dave C-J 2013-10-24 19:58:57 +01:00
parent f1f00da1a8
commit a4160a6bea
2 changed files with 84 additions and 78 deletions

View File

@ -63,6 +63,7 @@
<script type="text/x-red" data-help-name="udp in">
<p>A udp input node, that produces a <b>msg.payload</b> containing a <i>BUFFER</i>, string, or base64 encoded string. Supports multicast.</p>
<p>It also provides <b>msg.fromip</b> in the form ipaddress:port .</p>
<p>On some systems you may need to be root to use ports below 1024 and/or broadcast.</p>
</script>
<script type="text/javascript">
@ -145,8 +146,9 @@
<script type="text/x-red" data-help-name="udp out">
<p>This node sends <b>msg.payload</b> to the designated udp host and port. Supports multicast.</p>
<p>You may also use <b>msg.destip</b> and <b>msg.port</b> to override the values manually configured.</p>
<p>If you select broadcast either set the address to the local broadcast ip address, or maybe try 255.255.255.255, which is the global broadcast address.</p>
<p>On some systems you may need to be root to use broadcast.</p>
<p>On some systems you may need to be root to use ports below 1024 and/or broadcast.</p>
</script>
<script type="text/javascript">

View File

@ -31,7 +31,9 @@ function UDPin(n) {
var server = dgram.createSocket('udp4');
server.on("error", function (err) {
console.log("udp listener error:\n" + err.stack);
//console.log("udp listener error:\n" + err.stack);
if ((err.code == "EACCES") && (node.port < 1024)) { node.error("UDP access error, you may need root access for ports below 1024"); }
else { node.error("UDP error : "+err.code); }
server.close();
});
@ -94,11 +96,13 @@ function UDPout(n) {
node.on("input", function(msg) {
if (msg.payload != null) {
//console.log("UDP:",msg.payload);
var add = msg.destip || node.addr;
var por = msg.port || node.addr;
var message;
if (node.base64) { message = new Buffer(b64string, 'base64'); }
else { message = new Buffer(""+msg.payload); }
console.log("UDP send :",node.addr,node.port);
sock.send(message, 0, message.length, node.port, node.addr, function(err, bytes) {
//console.log("UDP send :",add,por);
sock.send(message, 0, message.length, por, add, function(err, bytes) {
if (err) node.error("udp : "+err);
});
}