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

Rework local port UI in udp node

This commit is contained in:
Nick O'Leary 2014-03-30 23:05:59 +01:00
parent e2bac40b17
commit a3fa6dada5
2 changed files with 78 additions and 31 deletions

View File

@ -103,7 +103,7 @@
<option value="broad">broadcast message</option>
<option value="multi">multicast message</option>
</select>
to port <input type="text" id="node-input-port" placeholder="Port" style="width: 70px">
to port <input type="text" id="node-input-port" placeholder="port" style="width: 70px">
</div>
<div class="form-row node-input-addr">
<label for="node-input-addr" id="node-input-addr-label"><i class="icon-list"></i> Address</label>
@ -114,8 +114,12 @@
<input type="text" id="node-input-iface" placeholder="(optional) ip address of eth0">
</div>
<div class="form-row">
<label for="node-input-outport"><i class="icon-random"></i> optional</label>
output port <input type="text" id="node-input-outport" style="width: 70px;" placeholder="'leave blank for random">
<label for="node-input-outport-type">&nbsp;</label>
<select id="node-input-outport-type">
<option value="random">use random local port</option>
<option value="fixed">bind to local port</option>
</select>
<input type="text" id="node-input-outport" style="width: 70px;" placeholder="port">
</div>
<div class="form-row">
<label>&nbsp;</label>
@ -130,7 +134,6 @@
<script>
$("#node-input-multicast").change(function() {
var id = $("#node-input-multicast option:selected").val();
console.log(id,$("#node-input-addr")[0].placeholder);
if (id !== "multi") {
$(".node-input-iface").hide();
$("#node-input-addr-label").html('<i class="icon-list"></i> Address');
@ -177,6 +180,25 @@
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var type = this.outport==""?"random":"fixed";
$("#node-input-outport-type option").filter(function() {
return $(this).val() == type;
}).attr('selected',true);
$("#node-input-outport-type").change(function() {
var type = $(this).children("option:selected").val();
if (type == "random") {
$("#node-input-outport").val("").hide();
} else {
$("#node-input-outport").show();
}
});
$("#node-input-outport-type").change();
}
});
</script>

View File

@ -30,17 +30,23 @@ function UDPin(n) {
var server = dgram.createSocket('udp4');
server.on("error", function (err) {
//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); }
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();
});
server.on('message', function (message, remote) {
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, ip:remote.address, port:remote.port }; }
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, ip:remote.address, port:remote.port };
}
node.send(msg);
});
@ -54,9 +60,13 @@ function UDPin(n) {
server.addMembership(node.group,node.iface);
node.log("udp multicast group "+node.group);
} catch (e) {
if (e.errno == "EINVAL") { node.error("Bad Multicast Address"); }
else if (e.errno == "ENODEV") { node.error("Must be ip address of the required interface"); }
else { node.error("Error :"+e.errno); }
if (e.errno == "EINVAL") {
node.error("Bad Multicast Address");
} else if (e.errno == "ENODEV") {
node.error("Must be ip address of the required interface");
} else {
node.error("Error :"+e.errno);
}
}
}
});
@ -65,8 +75,9 @@ function UDPin(n) {
try {
server.close();
node.log('udp listener stopped');
} catch (err) {
node.error(err);
}
catch (err) { console.log(err); }
});
server.bind(node.port,node.iface);
@ -98,35 +109,48 @@ function UDPout(n) {
sock.addMembership(node.addr,node.iface); // Add to the multicast group
node.log('udp multicast ready : '+node.outport+' -> '+node.addr+":"+node.port);
} catch (e) {
if (e.errno == "EINVAL") { node.error("Bad Multicast Address"); }
else if (e.errno == "ENODEV") { node.error("Must be ip address of the required interface"); }
else { node.error("Error :"+e.errno); }
if (e.errno == "EINVAL") {
node.error("Bad Multicast Address");
} else if (e.errno == "ENODEV") {
node.error("Must be ip address of the required interface");
} else {
node.error("Error :"+e.errno);
}
}
} else {
node.log('udp broadcast ready : '+node.outport+' -> '+node.addr+":"+node.port);
}
else node.log('udp broadcast ready : '+node.outport+' -> '+node.addr+":"+node.port);
});
}
else if (node.outport != "") {
} else if (node.outport != "") {
sock.bind(node.outport);
node.log('udp ready : '+node.outport+' -> '+node.addr+":"+node.port);
} else {
node.log('udp ready : '+node.addr+":"+node.port);
}
else node.log('udp ready : '+node.addr+":"+node.port);
node.on("input", function(msg) {
if (msg.payload != null) {
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 {
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,msg.payload.toString());
if (node.base64) {
message = new Buffer(b64string, 'base64');
} else if (msg.payload instanceof Buffer) {
message = msg.payload;
} else {
message = new Buffer(""+msg.payload);
}
sock.send(message, 0, message.length, por, add, function(err, bytes) {
if (err) node.error("udp : "+err);
if (err) {
node.error("udp : "+err);
}
});
}
}
@ -136,8 +160,9 @@ function UDPout(n) {
try {
sock.close();
node.log('udp output stopped');
} catch (err) {
node.error(err);
}
catch (err) { console.log(err); }
});
}
RED.nodes.registerType("udp out",UDPout);