mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 11:36:53 +00:00
Add socketTimeout to settings.js for TCP server sockets
Fixes #125 adds an optional socketTimeout param to settings.js file to add a TCP server socket timeout. Default is no timeout.
This commit is contained in:
parent
a9668a1999
commit
2ba5e0fe3e
@ -16,12 +16,11 @@
|
|||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||||
var reconnectTime = RED.settings.socketReconnectTime||10000;
|
var reconnectTime = RED.settings.socketReconnectTime||10000;
|
||||||
|
var socketTimeout = RED.settings.socketTimeout||null;
|
||||||
var net = require('net');
|
var net = require('net');
|
||||||
|
|
||||||
var connectionPool = {};
|
var connectionPool = {};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function TcpIn(n) {
|
function TcpIn(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.host = n.host;
|
this.host = n.host;
|
||||||
@ -83,7 +82,6 @@ function TcpIn(n) {
|
|||||||
buffer = null;
|
buffer = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('close', function() {
|
client.on('close', function() {
|
||||||
delete connectionPool[id];
|
delete connectionPool[id];
|
||||||
node.log("connection lost to "+node.host+":"+node.port);
|
node.log("connection lost to "+node.host+":"+node.port);
|
||||||
@ -91,7 +89,6 @@ function TcpIn(n) {
|
|||||||
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
|
reconnectTimeout = setTimeout(setupTcpClient, reconnectTime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', function(err) {
|
client.on('error', function(err) {
|
||||||
node.log(err);
|
node.log(err);
|
||||||
});
|
});
|
||||||
@ -105,6 +102,7 @@ function TcpIn(n) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var server = net.createServer(function (socket) {
|
var server = net.createServer(function (socket) {
|
||||||
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
var id = (1+Math.random()*4294967295).toString(16);
|
var id = (1+Math.random()*4294967295).toString(16);
|
||||||
connectionPool[id] = socket;
|
connectionPool[id] = socket;
|
||||||
|
|
||||||
@ -145,6 +143,10 @@ function TcpIn(n) {
|
|||||||
buffer = null;
|
buffer = null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
socket.on('timeout', function() {
|
||||||
|
node.log('timeout closed socket port '+node.port);
|
||||||
|
socket.end();
|
||||||
|
});
|
||||||
socket.on('close', function() {
|
socket.on('close', function() {
|
||||||
delete connectionPool[id];
|
delete connectionPool[id];
|
||||||
});
|
});
|
||||||
@ -163,7 +165,6 @@ function TcpIn(n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("tcp in",TcpIn);
|
RED.nodes.registerType("tcp in",TcpIn);
|
||||||
|
|
||||||
function TcpOut(n) {
|
function TcpOut(n) {
|
||||||
@ -187,14 +188,11 @@ function TcpOut(n) {
|
|||||||
connected = true;
|
connected = true;
|
||||||
node.log("connected to "+node.host+":"+node.port);
|
node.log("connected to "+node.host+":"+node.port);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) {
|
||||||
node.log('error : '+err);
|
node.log('error : '+err);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('end', function (err) {
|
client.on('end', function (err) {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('close', function() {
|
client.on('close', function() {
|
||||||
node.log("connection lost to "+node.host+":"+node.port);
|
node.log("connection lost to "+node.host+":"+node.port);
|
||||||
connected = false;
|
connected = false;
|
||||||
@ -206,7 +204,6 @@ function TcpOut(n) {
|
|||||||
}
|
}
|
||||||
setupTcpClient();
|
setupTcpClient();
|
||||||
|
|
||||||
|
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
if (connected && msg.payload != null) {
|
if (connected && msg.payload != null) {
|
||||||
if (Buffer.isBuffer(msg.payload)) {
|
if (Buffer.isBuffer(msg.payload)) {
|
||||||
@ -243,9 +240,14 @@ function TcpOut(n) {
|
|||||||
} else {
|
} else {
|
||||||
var connectedSockets = [];
|
var connectedSockets = [];
|
||||||
var server = net.createServer(function (socket) {
|
var server = net.createServer(function (socket) {
|
||||||
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
|
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
|
||||||
node.log("connection from "+remoteDetails);
|
node.log("connection from "+remoteDetails);
|
||||||
connectedSockets.push(socket);
|
connectedSockets.push(socket);
|
||||||
|
socket.on('timeout', function() {
|
||||||
|
node.log('timeout closed socket port '+node.port);
|
||||||
|
socket.end();
|
||||||
|
});
|
||||||
socket.on('close',function() {
|
socket.on('close',function() {
|
||||||
node.log("connection closed from "+remoteDetails);
|
node.log("connection closed from "+remoteDetails);
|
||||||
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
||||||
@ -254,7 +256,6 @@ function TcpOut(n) {
|
|||||||
node.log("socket error from "+remoteDetails);
|
node.log("socket error from "+remoteDetails);
|
||||||
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
if (msg.payload != null) {
|
if (msg.payload != null) {
|
||||||
@ -283,5 +284,3 @@ function TcpOut(n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("tcp out",TcpOut);
|
RED.nodes.registerType("tcp out",TcpOut);
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,6 +31,10 @@ module.exports = {
|
|||||||
// Retry time in milliseconds for TCP socket connections
|
// Retry time in milliseconds for TCP socket connections
|
||||||
//socketReconnectTime: 10000,
|
//socketReconnectTime: 10000,
|
||||||
|
|
||||||
|
// Timeout in milliseconds for TCP server socket connections
|
||||||
|
// defaults to no timeout
|
||||||
|
//socketTimeout: 120000,
|
||||||
|
|
||||||
// Maximum number of lines in debug window before pruning
|
// Maximum number of lines in debug window before pruning
|
||||||
debugMaxLength: 1000,
|
debugMaxLength: 1000,
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user