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

ensure udp socket bind works more often

This commit is contained in:
Dave Conway-Jones 2015-11-16 08:38:22 +00:00
parent 60812b2d8a
commit 43dad4c465

View File

@ -29,7 +29,7 @@ module.exports = function(RED) {
this.ipv = n.ipv || "udp4"; this.ipv = n.ipv || "udp4";
var node = this; var node = this;
var server = dgram.createSocket(node.ipv); // default to ipv4 var server = dgram.createSocket({type:node.ipv, reuseAddr:true}); // default to ipv4
server.on("error", function (err) { server.on("error", function (err) {
if ((err.code == "EACCES") && (node.port < 1024)) { if ((err.code == "EACCES") && (node.port < 1024)) {
@ -82,9 +82,7 @@ module.exports = function(RED) {
} }
}); });
// Hack for when you have both in and out udp nodes sharing a port server.bind(node.port,node.iface);
// if udp in starts last it shares better - so give it a chance to be last
setTimeout( function() { server.bind(node.port,node.iface); }, 250);
} }
RED.nodes.registerType("udp in",UDPin); RED.nodes.registerType("udp in",UDPin);
@ -102,7 +100,7 @@ module.exports = function(RED) {
this.ipv = n.ipv || "udp4"; this.ipv = n.ipv || "udp4";
var node = this; var node = this;
var sock = dgram.createSocket(node.ipv); // default to ipv4 var sock = dgram.createSocket({type:node.ipv, reuseAddr:true}); // default to ipv4
sock.on("error", function(err) { sock.on("error", function(err) {
// Any async error will also get reported in the sock.send call. // Any async error will also get reported in the sock.send call.
@ -133,8 +131,10 @@ module.exports = function(RED) {
} }
}); });
} else if (node.outport != "") { } else if (node.outport != "") {
sock.bind(node.outport); setTimeout( function() {
node.log(RED._("udp.status.ready",{outport:node.outport,host:node.addr,port:node.port})); sock.bind(node.outport);
node.log(RED._("udp.status.ready",{outport:node.outport,host:node.addr,port:node.port}));
}, 250);
} else { } else {
node.log(RED._("udp.status.ready-nolocal",{host:node.addr,port:node.port})); node.log(RED._("udp.status.ready-nolocal",{host:node.addr,port:node.port}));
} }