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

Merge pull request #3204 from node-red/tcp-req-text-output

Add string option to TCP request node output
This commit is contained in:
Nick O'Leary 2021-10-20 09:40:44 +01:00 committed by GitHub
commit fc9d65abcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 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.output.buffer"></option>
<option value="string" data-i18n="tcpin.output.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({});