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

Let tcprequest split incoming strings on delimiter (as per tcpin node)

and fixup i18n messages
This commit is contained in:
Dave Conway-Jones 2021-12-26 15:28:16 +00:00
parent 6692b1992c
commit cae247160f
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
3 changed files with 42 additions and 14 deletions

View File

@ -50,7 +50,7 @@
</div> </div>
<div id="node-row-newline" class="form-row hidden" style="padding-left:110px;"> <div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;"> <span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional">
</div> </div>
<div class="form-row"> <div class="form-row">
@ -274,7 +274,7 @@
</select> </select>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-out"> </label> <label for="node-input-out"><i class="fa fa-sign-out fa-rotate-90"></i> <span data-i18n="tcpin.label.close"></span></label>
<select type="text" id="node-input-out" style="width:54%;"> <select type="text" id="node-input-out" style="width:54%;">
<option value="time" data-i18n="tcpin.return.timeout"></option> <option value="time" data-i18n="tcpin.return.timeout"></option>
<option value="char" data-i18n="tcpin.return.character"></option> <option value="char" data-i18n="tcpin.return.character"></option>
@ -285,6 +285,9 @@
<input type="text" id="node-input-splitc" style="width:50px;"> <input type="text" id="node-input-splitc" style="width:50px;">
<span id="node-units"></span> <span id="node-units"></span>
</div> </div>
<div id="node-row-newline" class="form-row hidden" style="padding-left:162px;">
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional">
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -302,6 +305,7 @@
out: {value:"time", required:true}, out: {value:"time", required:true},
ret: {value:"buffer"}, ret: {value:"buffer"},
splitc: {value:"0", required:true}, splitc: {value:"0", required:true},
newline: {value:""},
tls: {type:"tls-config", value:'', required:false} tls: {type:"tls-config", value:'', required:false}
}, },
inputs:1, inputs:1,
@ -319,6 +323,14 @@
$("#node-input-ret").val("buffer"); $("#node-input-ret").val("buffer");
this.ret = "buffer"; this.ret = "buffer";
} }
$("#node-input-ret").on("change", function() {
if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
else { $("#node-row-newline").hide(); }
});
$("#node-input-out").on("change", function() {
if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
else { $("#node-row-newline").hide(); }
});
$("#node-input-out").on('focus', function () { previous = this.value; }).on("change", function() { $("#node-input-out").on('focus', function () { previous = this.value; }).on("change", function() {
$("#node-input-splitc").show(); $("#node-input-splitc").show();
if (previous === null) { previous = $("#node-input-out").val(); } if (previous === null) { previous = $("#node-input-out").val(); }

View File

@ -229,7 +229,7 @@ module.exports = function(RED) {
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) {
msg = {topic:node.topic, payload:parts[i], ip:socket.remoteAddress, port:socket.remotePort}; msg = {topic:node.topic, payload:parts[i] + node.newline.trimEnd(), ip:socket.remoteAddress, port:socket.remotePort};
msg._session = {type:"tcp",id:id}; msg._session = {type:"tcp",id:id};
node.send(msg); node.send(msg);
} }
@ -588,8 +588,7 @@ module.exports = function(RED) {
...connOpts ...connOpts
}; };
if (!options.keepAlive) if (!options.keepAlive) { options.singleUse = true; }
options.singleUse = true;
const context = options.secureContext || tls.createSecureContext(options); const context = options.secureContext || tls.createSecureContext(options);
@ -640,19 +639,34 @@ module.exports = function(RED) {
else { else {
node.warn(RED._("tcpin.errors.no-host")); node.warn(RED._("tcpin.errors.no-host"));
} }
var chunk = "";
clients[connection_id].client.on('data', function(data) { clients[connection_id].client.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
if (clients[connection_id]) { if (clients[connection_id]) {
const msg = clients[connection_id].lastMsg || {}; const msg = clients[connection_id].lastMsg || {};
msg.payload = RED.util.cloneMessage(data); msg.payload = RED.util.cloneMessage(data);
if (node.ret === "string") { if (node.ret === "string") {
try { msg.payload = msg.payload.toString(); } try {
catch(e) { node.error("Failed to create string", msg); } if (node.newline && node.newline !== "" ) {
chunk += msg.payload.toString();
let parts = chunk.split(node.newline);
for (var i=0; i<parts.length-1; i+=1) {
let m = RED.util.cloneMessage(msg);
m.payload = parts[i] + node.newline.trimEnd();
nodeSend(m);
} }
chunk = parts[parts.length-1];
}
else {
msg.payload = msg.payload.toString();
nodeSend(msg); nodeSend(msg);
} }
} }
catch(e) { node.error(RED._("tcpin.errors.bad-string"), msg); }
}
else { nodeSend(msg); }
}
}
// else if (node.splitc === 0) { // else if (node.splitc === 0) {
// clients[connection_id].msg.payload = data; // clients[connection_id].msg.payload = data;
// node.send(clients[connection_id].msg); // node.send(clients[connection_id].msg);

View File

@ -579,7 +579,9 @@
"server": "Server", "server": "Server",
"return": "Return", "return": "Return",
"ms": "ms", "ms": "ms",
"chars": "chars" "chars": "chars",
"close": "Close",
"optional": "(optional)"
}, },
"type": { "type": {
"listen": "Listen on", "listen": "Listen on",
@ -596,7 +598,7 @@
"return": { "return": {
"timeout": "after a fixed timeout of", "timeout": "after a fixed timeout of",
"character": "when character received is", "character": "when character received is",
"number": "a fixed number of chars", "number": "after a fixed number of characters",
"never": "never - keep connection open", "never": "never - keep connection open",
"immed": "immediately - don't wait for reply" "immed": "immediately - don't wait for reply"
}, },
@ -616,11 +618,11 @@
"timeout": "timeout closed socket port __port__", "timeout": "timeout closed socket port __port__",
"cannot-listen": "unable to listen on port __port__, error: __error__", "cannot-listen": "unable to listen on port __port__, error: __error__",
"error": "error: __error__", "error": "error: __error__",
"socket-error": "socket error from __host__:__port__", "socket-error": "socket error from __host__:__port__",
"no-host": "Host and/or port not set", "no-host": "Host and/or port not set",
"connect-timeout": "connect timeout", "connect-timeout": "connect timeout",
"connect-fail": "connect failed" "connect-fail": "connect failed",
"bad-string": "failed to convert to string"
} }
}, },
"udp": { "udp": {