From bd77d7eec358e090cc3eda1e4738ba606cb349fb Mon Sep 17 00:00:00 2001 From: Tobias Oort Date: Sat, 8 Jan 2022 21:12:59 +0100 Subject: [PATCH 1/3] Implemented support for Websocket Subprotocols in WS Client Node. --- .../nodes/core/network/22-websocket.html | 8 +++++-- .../nodes/core/network/22-websocket.js | 3 ++- .../@node-red/nodes/locales/de/messages.json | 3 ++- .../nodes/locales/en-US/messages.json | 3 ++- .../@node-red/nodes/locales/ja/messages.json | 3 ++- .../@node-red/nodes/locales/ko/messages.json | 3 ++- .../@node-red/nodes/locales/ru/messages.json | 3 ++- .../nodes/locales/zh-CN/messages.json | 3 ++- .../nodes/locales/zh-TW/messages.json | 3 ++- test/nodes/core/network/22-websocket_spec.js | 24 +++++++++++++++++++ 10 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/22-websocket.html b/packages/node_modules/@node-red/nodes/core/network/22-websocket.html index e3ee30eeb..8a97c50ab 100644 --- a/packages/node_modules/@node-red/nodes/core/network/22-websocket.html +++ b/packages/node_modules/@node-red/nodes/core/network/22-websocket.html @@ -177,7 +177,8 @@ path: {value:"",required:true,validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/)}, tls: {type:"tls-config",required: false}, wholemsg: {value:"false"}, - hb: {value: "", validate: RED.validators.number(/*blank allowed*/true) } + hb: {value: "", validate: RED.validators.number(/*blank allowed*/true) }, + subprotocol: {value:"",required: false} }, inputs:0, outputs:0, @@ -265,7 +266,10 @@ - +
+ + +
+
diff --git a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js index c22e3dea1..0ceac5b01 100644 --- a/packages/node_modules/@node-red/nodes/core/network/22-websocket.js +++ b/packages/node_modules/@node-red/nodes/core/network/22-websocket.js @@ -46,7 +46,12 @@ module.exports = function(RED) { // Store local copies of the node configuration (as defined in the .html) node.path = n.path; - node.subprotocol = n.subprotocol; // optional client protocol + if (typeof n.subprotocol === "string") { + // Split the string on comma and trim each result + node.subprotocol = n.subprotocol.split(",").map(v => v.trim()) + } else { + node.subprotocol = []; + } node.wholemsg = (n.wholemsg === "true"); node._inputNodes = []; // collection of nodes that want to receive events diff --git a/test/nodes/core/network/22-websocket_spec.js b/test/nodes/core/network/22-websocket_spec.js index 4ab96f8ff..21f415531 100644 --- a/test/nodes/core/network/22-websocket_spec.js +++ b/test/nodes/core/network/22-websocket_spec.js @@ -370,10 +370,10 @@ describe('websocket Node', function() { var flow = [ { id: "server", type: "websocket-listener", path: "/ws" }, { id: "n1", type: "websocket-client", path: getWsUrl("/ws") }, - { id: "n2", type: "websocket-client", path: getWsUrl("/ws"), subprotocol: "testprotocol" }]; + { id: "n2", type: "websocket-client", path: getWsUrl("/ws"), subprotocol: "testprotocol1, testprotocol2" }]; helper.load(websocketNode, flow, function() { - helper.getNode("n1").should.have.property("subprotocol", undefined); - helper.getNode("n2").should.have.property("subprotocol", "testprotocol"); + helper.getNode("n1").should.have.property("subprotocol", []); + helper.getNode("n2").should.have.property("subprotocol", ["testprotocol1","testprotocol2"]); done(); }); });