mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
merge from upstream
This commit is contained in:
commit
b81f251023
@ -25,7 +25,7 @@ function FunctionNode(n) {
|
|||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.name = n.name;
|
this.name = n.name;
|
||||||
this.func = n.func;
|
this.func = n.func;
|
||||||
var functionText = "var results = (function(msg){"+this.func+"})(msg);";
|
var functionText = "var results = (function(msg){"+this.func+"\n})(msg);";
|
||||||
this.topic = n.topic;
|
this.topic = n.topic;
|
||||||
this.context = {global:RED.settings.functionGlobalContext || {}};
|
this.context = {global:RED.settings.functionGlobalContext || {}};
|
||||||
try {
|
try {
|
||||||
|
@ -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;
|
||||||
@ -43,55 +42,53 @@ function TcpIn(n) {
|
|||||||
node.log("connecting to "+node.host+":"+node.port);
|
node.log("connecting to "+node.host+":"+node.port);
|
||||||
var id = (1+Math.random()*4294967295).toString(16);
|
var id = (1+Math.random()*4294967295).toString(16);
|
||||||
client = net.connect(node.port, node.host, function() {
|
client = net.connect(node.port, node.host, function() {
|
||||||
buffer = (node.datatype == 'buffer')? new Buffer(0):"";
|
buffer = (node.datatype == 'buffer')? new Buffer(0):"";
|
||||||
node.log("connected to "+node.host+":"+node.port);
|
node.log("connected to "+node.host+":"+node.port);
|
||||||
});
|
});
|
||||||
connectionPool[id] = client;
|
connectionPool[id] = client;
|
||||||
|
|
||||||
client.on('data', function (data) {
|
client.on('data', function (data) {
|
||||||
if (node.datatype != 'buffer') {
|
if (node.datatype != 'buffer') {
|
||||||
data = data.toString(node.datatype);
|
data = data.toString(node.datatype);
|
||||||
}
|
}
|
||||||
if (node.stream) {
|
if (node.stream) {
|
||||||
if ((node.datatype) === "utf8" && node.newline != "") {
|
if ((node.datatype) === "utf8" && node.newline != "") {
|
||||||
buffer = buffer+data;
|
buffer = buffer+data;
|
||||||
var parts = buffer.split(node.newline);
|
var parts = buffer.split(node.newline);
|
||||||
for (var i = 0;i<parts.length-1;i+=1) {
|
for (var i = 0;i<parts.length-1;i+=1) {
|
||||||
var msg = {topic:node.topic, payload:parts[i]};
|
var msg = {topic:node.topic, payload:parts[i]};
|
||||||
msg._session = {type:"tcp",id:id};
|
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
buffer = parts[parts.length-1];
|
|
||||||
} else {
|
|
||||||
var msg = {topic:node.topic, payload:data};
|
|
||||||
msg._session = {type:"tcp",id:id};
|
msg._session = {type:"tcp",id:id};
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
|
buffer = parts[parts.length-1];
|
||||||
} else {
|
} else {
|
||||||
if ((typeof data) === "string") {
|
var msg = {topic:node.topic, payload:data};
|
||||||
buffer = buffer+data;
|
|
||||||
} else {
|
|
||||||
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
client.on('end', function() {
|
|
||||||
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) {
|
|
||||||
var msg = {topic:node.topic,payload:buffer};
|
|
||||||
msg._session = {type:"tcp",id:id};
|
msg._session = {type:"tcp",id:id};
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
buffer = null;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if ((typeof data) === "string") {
|
||||||
|
buffer = buffer+data;
|
||||||
|
} else {
|
||||||
|
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
client.on('end', function() {
|
||||||
|
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) {
|
||||||
|
var msg = {topic:node.topic,payload:buffer};
|
||||||
|
msg._session = {type:"tcp",id:id};
|
||||||
|
node.send(msg);
|
||||||
|
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);
|
||||||
if (!node.closing) {
|
if (!node.closing) {
|
||||||
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,52 +102,57 @@ function TcpIn(n) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var server = net.createServer(function (socket) {
|
var server = net.createServer(function (socket) {
|
||||||
var id = (1+Math.random()*4294967295).toString(16);
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
connectionPool[id] = socket;
|
var id = (1+Math.random()*4294967295).toString(16);
|
||||||
|
connectionPool[id] = socket;
|
||||||
|
|
||||||
var buffer = (node.datatype == 'buffer')? new Buffer(0):"";
|
var buffer = (node.datatype == 'buffer')? new Buffer(0):"";
|
||||||
socket.on('data', function (data) {
|
socket.on('data', function (data) {
|
||||||
if (node.datatype != 'buffer') {
|
if (node.datatype != 'buffer') {
|
||||||
data = data.toString(node.datatype);
|
data = data.toString(node.datatype);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.stream) {
|
if (node.stream) {
|
||||||
if ((typeof data) === "string" && node.newline != "") {
|
if ((typeof data) === "string" && node.newline != "") {
|
||||||
buffer = buffer+data;
|
buffer = buffer+data;
|
||||||
var parts = buffer.split(node.newline);
|
var parts = buffer.split(node.newline);
|
||||||
for (var i = 0;i<parts.length-1;i+=1) {
|
for (var i = 0;i<parts.length-1;i+=1) {
|
||||||
var msg = {topic:node.topic, payload:parts[i],ip:socket.remoteAddress,port:socket.remotePort};
|
var msg = {topic:node.topic, payload:parts[i],ip:socket.remoteAddress,port:socket.remotePort};
|
||||||
msg._session = {type:"tcp",id:id};
|
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
buffer = parts[parts.length-1];
|
|
||||||
} else {
|
|
||||||
var msg = {topic:node.topic, payload:data};
|
|
||||||
msg._session = {type:"tcp",id:id};
|
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ((typeof data) === "string") {
|
|
||||||
buffer = buffer+data;
|
|
||||||
} else {
|
|
||||||
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
socket.on('end', function() {
|
|
||||||
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) {
|
|
||||||
var msg = {topic:node.topic,payload:buffer};
|
|
||||||
msg._session = {type:"tcp",id:id};
|
msg._session = {type:"tcp",id:id};
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
buffer = null;
|
|
||||||
}
|
}
|
||||||
});
|
buffer = parts[parts.length-1];
|
||||||
socket.on('close', function() {
|
} else {
|
||||||
delete connectionPool[id];
|
var msg = {topic:node.topic, payload:data};
|
||||||
});
|
msg._session = {type:"tcp",id:id};
|
||||||
socket.on('error',function(err) {
|
node.send(msg);
|
||||||
node.log(err);
|
}
|
||||||
});
|
} else {
|
||||||
|
if ((typeof data) === "string") {
|
||||||
|
buffer = buffer+data;
|
||||||
|
} else {
|
||||||
|
buffer = Buffer.concat([buffer,data],buffer.length+data.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.on('end', function() {
|
||||||
|
if (!node.stream || (node.datatype == "utf8" && node.newline != "" && buffer.length > 0)) {
|
||||||
|
var msg = {topic:node.topic,payload:buffer};
|
||||||
|
msg._session = {type:"tcp",id:id};
|
||||||
|
node.send(msg);
|
||||||
|
buffer = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.on('timeout', function() {
|
||||||
|
node.log('timeout closed socket port '+node.port);
|
||||||
|
socket.end();
|
||||||
|
});
|
||||||
|
socket.on('close', function() {
|
||||||
|
delete connectionPool[id];
|
||||||
|
});
|
||||||
|
socket.on('error',function(err) {
|
||||||
|
node.log(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
server.listen(node.port);
|
server.listen(node.port);
|
||||||
node.log('listening on port '+node.port);
|
node.log('listening on port '+node.port);
|
||||||
@ -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) {
|
||||||
@ -184,39 +185,35 @@ function TcpOut(n) {
|
|||||||
function setupTcpClient() {
|
function setupTcpClient() {
|
||||||
node.log("connecting to "+node.host+":"+node.port);
|
node.log("connecting to "+node.host+":"+node.port);
|
||||||
client = net.connect(node.port, node.host, function() {
|
client = net.connect(node.port, node.host, function() {
|
||||||
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;
|
||||||
client.destroy();
|
client.destroy();
|
||||||
if (!node.closing) {
|
if (!node.closing) {
|
||||||
reconnectTimeout = setTimeout(setupTcpClient,reconnectTime);
|
reconnectTimeout = setTimeout(setupTcpClient,reconnectTime);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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)) {
|
||||||
client.write(msg.payload);
|
client.write(msg.payload);
|
||||||
} else if (typeof msg.payload === "string" && node.base64) {
|
} else if (typeof msg.payload === "string" && node.base64) {
|
||||||
client.write(new Buffer(msg.payload,'base64'));
|
client.write(new Buffer(msg.payload,'base64'));
|
||||||
} else {
|
} else {
|
||||||
client.write(new Buffer(""+msg.payload));
|
client.write(new Buffer(""+msg.payload));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
node.on("close", function() {
|
node.on("close", function() {
|
||||||
@ -243,33 +240,37 @@ function TcpOut(n) {
|
|||||||
} else {
|
} else {
|
||||||
var connectedSockets = [];
|
var connectedSockets = [];
|
||||||
var server = net.createServer(function (socket) {
|
var server = net.createServer(function (socket) {
|
||||||
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
node.log("connection from "+remoteDetails);
|
var remoteDetails = socket.remoteAddress+":"+socket.remotePort;
|
||||||
connectedSockets.push(socket);
|
node.log("connection from "+remoteDetails);
|
||||||
socket.on('close',function() {
|
connectedSockets.push(socket);
|
||||||
node.log("connection closed from "+remoteDetails);
|
socket.on('timeout', function() {
|
||||||
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
node.log('timeout closed socket port '+node.port);
|
||||||
});
|
socket.end();
|
||||||
socket.on('error',function() {
|
});
|
||||||
node.log("socket error from "+remoteDetails);
|
socket.on('close',function() {
|
||||||
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
node.log("connection closed from "+remoteDetails);
|
||||||
});
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
||||||
|
});
|
||||||
|
socket.on('error',function() {
|
||||||
|
node.log("socket error from "+remoteDetails);
|
||||||
|
connectedSockets.splice(connectedSockets.indexOf(socket),1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
if (msg.payload != null) {
|
if (msg.payload != null) {
|
||||||
var buffer;
|
var buffer;
|
||||||
if (Buffer.isBuffer(msg.payload)) {
|
if (Buffer.isBuffer(msg.payload)) {
|
||||||
buffer = msg.payload;
|
buffer = msg.payload;
|
||||||
} else if (typeof msg.payload === "string" && node.base64) {
|
} else if (typeof msg.payload === "string" && node.base64) {
|
||||||
buffer = new Buffer(msg.payload,'base64');
|
buffer = new Buffer(msg.payload,'base64');
|
||||||
} else {
|
} else {
|
||||||
buffer = new Buffer(""+msg.payload);
|
buffer = new Buffer(""+msg.payload);
|
||||||
}
|
|
||||||
for (var i = 0; i<connectedSockets.length;i+=1) {
|
|
||||||
connectedSockets[i].write(buffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
for (var i = 0; i<connectedSockets.length;i+=1) {
|
||||||
|
connectedSockets[i].write(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(node.port);
|
server.listen(node.port);
|
||||||
@ -283,5 +284,3 @@ function TcpOut(n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("tcp out",TcpOut);
|
RED.nodes.registerType("tcp out",TcpOut);
|
||||||
|
|
||||||
|
|
||||||
|
@ -283,10 +283,12 @@ RED.app.get('/twitter/:id/auth', function(req, res){
|
|||||||
oauth_callback: req.query.callback
|
oauth_callback: req.query.callback
|
||||||
},function(error, oauth_token, oauth_token_secret, results){
|
},function(error, oauth_token, oauth_token_secret, results){
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log(error);
|
var resp = '<h2>Oh no!</h2>'+
|
||||||
res.send("yeah no. didn't work.")
|
'<p>Something went wrong with the authentication process. The following error was returned:<p>'+
|
||||||
}
|
'<p><b>'+error.statusCode+'</b>: '+error.data+'</p>'+
|
||||||
else {
|
'<p>One known cause of this type of failure is if the clock is wrong on system running Node-RED.';
|
||||||
|
res.send(resp)
|
||||||
|
} else {
|
||||||
credentials.oauth_token = oauth_token;
|
credentials.oauth_token = oauth_token;
|
||||||
credentials.oauth_token_secret = oauth_token_secret;
|
credentials.oauth_token_secret = oauth_token_secret;
|
||||||
res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token)
|
res.redirect('https://twitter.com/oauth/authorize?oauth_token='+oauth_token)
|
||||||
|
@ -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