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

Better fix to Close Issue#48. udp out node now accept msg.ip and msg.port properties - and udp in node now produces them also. Note- the ststically configured vlues WILL take precedence - so if you want to use msg.ip and msg.port then do not configure them in the edit dialog

This commit is contained in:
Dave C-J 2013-10-25 11:27:21 +01:00
parent a4160a6bea
commit 6fb8506722
2 changed files with 21 additions and 16 deletions

View File

@ -62,7 +62,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>It also provides <b>msg.ip</b> and <b>msg.port</b> to the ip address and port from which the message was received.</b>
<p>On some systems you may need to be root to use ports below 1024 and/or broadcast.</p>
</script>
@ -123,6 +123,7 @@
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Tip: leave address and port blank if you want to set using <b>msg.ip</b> and <b>msg.port</b>.</div>
<script>
$("#node-input-multicast").change(function() {
var id = $("#node-input-multicast option:selected").val();
@ -146,7 +147,7 @@
<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>You may also use <b>msg.ip</b> and <b>msg.port</b> to set the destination values.<br/><b>Note</b>: the statically configured values have precedence.</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 ports below 1024 and/or broadcast.</p>
</script>
@ -157,10 +158,9 @@
color:"Silver",
defaults: {
name: {value:""},
addr: {value:"",required:true},
//group: {value:""},
addr: {value:""},
iface: {value:""},
port: {value:"",required:true,validate:RED.validators.number()},
port: {value:""},
base64: {value:false,required:true},
multicast: {value:"false"}
},

View File

@ -41,7 +41,7 @@ function UDPin(n) {
var msg;
if (node.datatype =="base64") { msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port }; }
else if (node.datatype =="utf8") { msg = { payload:message.toString('utf8'), fromip:remote.address+':'+remote.port }; }
else { msg = { payload:message, fromip:remote.address+':'+remote.port }; }
else { msg = { payload:message, fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port }; }
node.send(msg);
});
@ -95,17 +95,22 @@ 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 add = node.addr || msg.ip || "";
var por = node.port || msg.port || 0;
if (add == "") { node.warn("udp: ip address not set"); }
else if (por == 0) { node.warn("udp: port not set"); }
else if (isNaN(por) || (por < 1) || (por > 65535)) { node.warn("udp: port number not valid"); }
else {
var message;
if (node.base64) { message = new Buffer(b64string, 'base64'); }
else if (msg.payload instanceof Buffer) { message = msg.payload; }
else { message = new Buffer(""+msg.payload); }
//console.log("UDP send :",add,por);
console.log("UDP send :",add,por,msg.payload.toString());
sock.send(message, 0, message.length, por, add, function(err, bytes) {
if (err) node.error("udp : "+err);
});
}
}
});
node.on("close", function() {