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

Works when connection is left open

This commit is contained in:
Nathanaël Lécaudé 2016-11-09 13:25:19 -05:00
parent aef2c9e5cf
commit d56fce37dd

View File

@ -386,6 +386,7 @@ module.exports = function(RED) {
RED.nodes.registerType("tcp out",TcpOut); RED.nodes.registerType("tcp out",TcpOut);
var clients = {}; var clients = {};
function TcpGet(n) { function TcpGet(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.server = n.server; this.server = n.server;
@ -410,19 +411,18 @@ module.exports = function(RED) {
this.connected = false; this.connected = false;
var node = this; var node = this;
var m;
this.on("input", function(msg) { this.on("input", function(msg) {
m = msg;
var i = 0; var i = 0;
if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) { if ((!Buffer.isBuffer(msg.payload)) && (typeof msg.payload !== "string")) {
msg.payload = msg.payload.toString(); msg.payload = msg.payload.toString();
} }
if (!node.connected) {
var host = node.server || msg.host;
var port = node.port || msg.port;
node.connection_id = host + ":" + port;
var host = node.server || msg.host;
var port = node.port || msg.port;
node.connection_id = host + ":" + port;
if (!node.connected) {
var buf; var buf;
if (this.out == "count") { if (this.out == "count") {
if (this.splitc === 0) { buf = new Buffer(1); } if (this.splitc === 0) { buf = new Buffer(1); }
@ -431,7 +431,6 @@ module.exports = function(RED) {
else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully else { buf = new Buffer(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
clients[node.connection_id] = net.Socket(); clients[node.connection_id] = net.Socket();
node.warn('timeout ' + socketTimeout);
if (socketTimeout !== null) { clients[node.connection_id].setTimeout(socketTimeout); } if (socketTimeout !== null) { clients[node.connection_id].setTimeout(socketTimeout); }
if (host && port) { if (host && port) {
@ -449,12 +448,12 @@ module.exports = function(RED) {
clients[node.connection_id].on('data', function(data) { clients[node.connection_id].on('data', function(data) {
if (node.out == "sit") { // if we are staying connected just send the buffer if (node.out == "sit") { // if we are staying connected just send the buffer
m.payload = data; msg.payload = data;
node.send(m); node.send(RED.util.cloneMessage(msg));
} }
else if (node.splitc === 0) { else if (node.splitc === 0) {
msg.payload = data; msg.payload = data;
node.send(msg); node.send(RED.util.cloneMessage(msg));
} }
else { else {
for (var j = 0; j < data.length; j++ ) { for (var j = 0; j < data.length; j++ ) {
@ -470,7 +469,10 @@ module.exports = function(RED) {
msg.payload = new Buffer(i+1); msg.payload = new Buffer(i+1);
buf.copy(msg.payload,0,0,i+1); buf.copy(msg.payload,0,0,i+1);
node.send(msg); node.send(msg);
if (clients[cid]) { node.status({}); clients[cid].destroy(); } if (clients[cid]) {
node.status({}); clients[cid].destroy();
delete clients[cid];
}
}, node.splitc); }, node.splitc);
i = 0; i = 0;
buf[0] = data[j]; buf[0] = data[j];
@ -484,7 +486,10 @@ module.exports = function(RED) {
msg.payload = new Buffer(i); msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
node.send(msg); node.send(msg);
if (clients[cid]) { node.status({}); clients[cid].destroy(); } if (clients[cid]) {
node.status({}); clients[cid].destroy();
delete clients[cid];
}
i = 0; i = 0;
} }
} }
@ -496,7 +501,10 @@ module.exports = function(RED) {
msg.payload = new Buffer(i); msg.payload = new Buffer(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
node.send(msg); node.send(msg);
if (clients[cid]) { node.status({}); clients[cid].destroy(); } if (clients[cid]) {
node.status({}); clients[cid].destroy();
delete clients[cid];
}
i = 0; i = 0;
} }
} }
@ -522,11 +530,13 @@ module.exports = function(RED) {
node.connected = false; node.connected = false;
node.status({fill:"red",shape:"ring",text:"common.status.error"}); node.status({fill:"red",shape:"ring",text:"common.status.error"});
node.error(RED._("tcpin.errors.connect-fail"),msg); node.error(RED._("tcpin.errors.connect-fail"),msg);
if (clients[cid]) { clients[cid].destroy(); } if (clients[cid]) {
clients[cid].destroy();
delete clients[cid];
}
}); });
clients[node.connection_id].on('timeout',function() { clients[node.connection_id].on('timeout',function() {
//console.log("TIMEOUT");
node.connected = false; node.connected = false;
node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"}); node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"});
//node.warn(RED._("tcpin.errors.connect-timeout")); //node.warn(RED._("tcpin.errors.connect-timeout"));
@ -538,13 +548,16 @@ module.exports = function(RED) {
} }
}); });
} }
else { clients[node.connection_id].write(msg.payload); } else {
clients[node.connection_id].write(msg.payload);
}
}); });
this.on("close", function(done) { this.on("close", function(done) {
node.done = done; node.done = done;
if (clients[cid]) { if (clients[node.connection_id]) {
clients[node.connection_id].destroy(); clients[node.connection_id].destroy();
delete clients[node.connection_id];
} }
node.status({}); node.status({});
if (!node.connected) { done(); } if (!node.connected) { done(); }