Add optional string type output to tcp request node

to be similar to tcp in. node
This commit is contained in:
Dave Conway-Jones 2021-10-19 21:34:23 +01:00
parent 8d79deffb5
commit d3f978c90c
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
3 changed files with 36 additions and 5 deletions

View File

@ -196,6 +196,13 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-out"><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.return"></span></label> <label for="node-input-out"><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.return"></span></label>
<select type="text" id="node-input-ret" style="width:54%;">
<option value="buffer" data-i18n="tcpin.return.buffer"></option>
<option value="string" data-i18n="tcpin.return.string"></option>
</select>
</div>
<div class="form-row">
<label for="node-input-out"> </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>
@ -220,6 +227,7 @@
server: {value:""}, server: {value:""},
port: {value:"",validate:RED.validators.regex(/^(\d*|)$/)}, port: {value:"",validate:RED.validators.regex(/^(\d*|)$/)},
out: {value:"time",required:true}, out: {value:"time",required:true},
ret: {value:"buffer"},
splitc: {value:"0",required:true}, splitc: {value:"0",required:true},
name: {value:""} name: {value:""}
}, },
@ -234,6 +242,10 @@
}, },
oneditprepare: function() { oneditprepare: function() {
var previous = null; var previous = null;
if ($("#node-input-ret").val() == undefined) {
$("#node-input-ret").val("buffer");
this.ret = "buffer";
}
$("#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

@ -311,7 +311,7 @@ module.exports = function(RED) {
} }
setupTcpClient(); setupTcpClient();
node.on("input", function(msg,nodeSend,nodeDone) { node.on("input", function(msg, nodeSend, nodeDone) {
if (node.connected && msg.payload != null) { if (node.connected && msg.payload != null) {
if (Buffer.isBuffer(msg.payload)) { if (Buffer.isBuffer(msg.payload)) {
client.write(msg.payload); client.write(msg.payload);
@ -444,6 +444,7 @@ module.exports = function(RED) {
this.server = n.server; this.server = n.server;
this.port = Number(n.port); this.port = Number(n.port);
this.out = n.out; this.out = n.out;
this.ret = n.ret || "buffer";
this.splitc = n.splitc; this.splitc = n.splitc;
if (this.out === "immed") { this.splitc = -1; this.out = "time"; } if (this.out === "immed") { this.splitc = -1; this.out = "time"; }
@ -488,7 +489,7 @@ module.exports = function(RED) {
connected: false, connected: false,
connecting: false connecting: false
}; };
enqueue(clients[connection_id].msgQueue, {msg:msg,nodeSend:nodeSend, nodeDone: nodeDone}); enqueue(clients[connection_id].msgQueue, {msg:msg, nodeSend:nodeSend, nodeDone:nodeDone});
clients[connection_id].lastMsg = msg; clients[connection_id].lastMsg = msg;
if (!clients[connection_id].connecting && !clients[connection_id].connected) { if (!clients[connection_id].connecting && !clients[connection_id].connected) {
@ -532,8 +533,12 @@ module.exports = function(RED) {
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 = data; msg.payload = RED.util.cloneMessage(data);
nodeSend(RED.util.cloneMessage(msg)); if (node.ret === "string") {
try { msg.payload = msg.payload.toString(); }
catch(e) { node.error("Failed to create string", msg); }
}
nodeSend(msg);
} }
} }
// else if (node.splitc === 0) { // else if (node.splitc === 0) {
@ -556,6 +561,10 @@ module.exports = function(RED) {
const msg = clients[connection_id].lastMsg || {}; const msg = clients[connection_id].lastMsg || {};
msg.payload = Buffer.alloc(i+1); msg.payload = Buffer.alloc(i+1);
buf.copy(msg.payload,0,0,i+1); buf.copy(msg.payload,0,0,i+1);
if (node.ret === "string") {
try { msg.payload = msg.payload.toString(); }
catch(e) { node.error("Failed to create string", msg); }
}
nodeSend(msg); nodeSend(msg);
if (clients[connection_id].client) { if (clients[connection_id].client) {
node.status({}); node.status({});
@ -578,6 +587,10 @@ module.exports = function(RED) {
const msg = clients[connection_id].lastMsg || {}; const msg = clients[connection_id].lastMsg || {};
msg.payload = Buffer.alloc(i); msg.payload = Buffer.alloc(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
if (node.ret === "string") {
try { msg.payload = msg.payload.toString(); }
catch(e) { node.error("Failed to create string", msg); }
}
nodeSend(msg); nodeSend(msg);
if (clients[connection_id].client) { if (clients[connection_id].client) {
node.status({}); node.status({});
@ -597,6 +610,10 @@ module.exports = function(RED) {
const msg = clients[connection_id].lastMsg || {}; const msg = clients[connection_id].lastMsg || {};
msg.payload = Buffer.alloc(i); msg.payload = Buffer.alloc(i);
buf.copy(msg.payload,0,0,i); buf.copy(msg.payload,0,0,i);
if (node.ret === "string") {
try { msg.payload = msg.payload.toString(); }
catch(e) { node.error("Failed to create string", msg); }
}
nodeSend(msg); nodeSend(msg);
if (clients[connection_id].client) { if (clients[connection_id].client) {
node.status({}); node.status({});

View File

@ -598,7 +598,9 @@
"character": "when character received is", "character": "when character received is",
"number": "a fixed number of chars", "number": "a fixed number of chars",
"never": "never - keep connection open", "never": "never - keep connection open",
"immed": "immediately - don't wait for reply" "immed": "immediately - don't wait for reply",
"buffer": "a Buffer",
"string": "a String"
}, },
"status": { "status": {
"connecting": "connecting to __host__:__port__", "connecting": "connecting to __host__:__port__",