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

add a default keepalive to tcp client mode

to address #1469
This commit is contained in:
Dave Conway-Jones 2018-02-06 11:36:09 +00:00
parent 5931e13b9c
commit 0968f96982
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4

View File

@ -52,6 +52,7 @@ module.exports = function(RED) {
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
client.setKeepAlive(true,120000);
connectionPool[id] = client; connectionPool[id] = client;
client.on('data', function (data) { client.on('data', function (data) {
@ -123,7 +124,8 @@ module.exports = function(RED) {
clearTimeout(reconnectTimeout); clearTimeout(reconnectTimeout);
if (!node.connected) { done(); } if (!node.connected) { done(); }
}); });
} else { }
else {
var server = net.createServer(function (socket) { var server = net.createServer(function (socket) {
socket.setKeepAlive(true,120000); socket.setKeepAlive(true,120000);
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); } if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
@ -184,6 +186,7 @@ module.exports = function(RED) {
node.log(err); node.log(err);
}); });
}); });
server.on('error', function(err) { server.on('error', function(err) {
if (err) { if (err) {
node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()})); node.error(RED._("tcpin.errors.cannot-listen",{port:node.port,error:err.toString()}));
@ -237,6 +240,7 @@ module.exports = function(RED) {
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port})); node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
node.status({fill:"green",shape:"dot",text:"common.status.connected"}); node.status({fill:"green",shape:"dot",text:"common.status.connected"});
}); });
client.setKeepAlive(true,120000);
client.on('error', function (err) { client.on('error', function (err) {
node.log(RED._("tcpin.errors.error",{error:err.toString()})); node.log(RED._("tcpin.errors.error",{error:err.toString()}));
}); });
@ -288,7 +292,8 @@ module.exports = function(RED) {
if (!node.connected) { done(); } if (!node.connected) { done(); }
}); });
} else if (node.beserver == "reply") { }
else if (node.beserver == "reply") {
node.on("input",function(msg) { node.on("input",function(msg) {
if (msg._session && msg._session.type == "tcp") { if (msg._session && msg._session.type == "tcp") {
var client = connectionPool[msg._session.id]; var client = connectionPool[msg._session.id];
@ -314,7 +319,8 @@ module.exports = function(RED) {
} }
} }
}); });
} else { }
else {
var connectedSockets = []; var connectedSockets = [];
node.status({text:RED._("tcpin.status.connections",{count:0})}); node.status({text:RED._("tcpin.status.connections",{count:0})});
var server = net.createServer(function (socket) { var server = net.createServer(function (socket) {