Only connect websocket client if need to

to address Issue #5506
only-connect-websocket-client-if-need-to
Dave Conway-Jones 2026-03-06 15:00:03 +00:00
parent f71bc1a268
commit 4349b58dc5
No known key found for this signature in database
GPG Key ID: 1DDB0E91A28C2643
1 changed files with 16 additions and 8 deletions

View File

@ -67,7 +67,7 @@ module.exports = function(RED) {
}
}
function startconn() { // Connect to remote endpoint
node.startconn = function() { // Connect to remote endpoint
node.tout = null;
const prox = getProxyForUrl(node.brokerurl, RED.settings.proxyOptions);
let agent = undefined;
@ -96,15 +96,15 @@ module.exports = function(RED) {
const keyValue = header.keyValue;
const valueType = header.valueType;
const valueValue = header.valueValue;
const headerName = keyType === 'other' ? keyValue : keyType;
let headerValue;
switch(valueType){
case 'other':
headerValue = valueValue;
break;
case 'env':
headerValue = RED.util.evaluateNodeProperty(valueValue,valueType,node);
break;
@ -165,7 +165,7 @@ module.exports = function(RED) {
}
if (!node.closing && !node.isServer) {
clearTimeout(node.tout);
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
node.tout = setTimeout(function() { node.startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
}
});
socket.on('message',function(data,flags) {
@ -176,7 +176,7 @@ module.exports = function(RED) {
node.emit('erro',{err:err,id:id});
if (!node.closing && !node.isServer) {
clearTimeout(node.tout);
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
node.tout = setTimeout(function() { node.startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
}
}
socket.on('error',socket.nrErrorHandler);
@ -229,7 +229,7 @@ module.exports = function(RED) {
}
else {
node.closing = false;
startconn(); // start outbound connection
// node.startconn(); // start outbound connection
}
node.on("close", function(done) {
@ -245,7 +245,7 @@ module.exports = function(RED) {
else {
node.closing = true;
node.server.close();
//wait 20*50 (1000ms max) for ws to close.
//wait 20*50 (1000ms max) for ws to close.
//call done when readyState === ws.CLOSED (or 1000ms, whichever comes fist)
const closeMonitorInterval = 20;
let closeMonitorCount = 50;
@ -268,6 +268,10 @@ module.exports = function(RED) {
WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler) {
this._inputNodes.push(handler);
// start connection if we are the first and we are a client
if (!this.isServer && this._inputNodes.length === 1) {
this.startconn();
}
}
WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) {
@ -417,6 +421,10 @@ module.exports = function(RED) {
status._session = {type:"websocket",id:event.id}
node.status(status);
});
// start connection if we are the first and we are a client
if (!this.serverConfig.isServer && this.serverConfig._inputNodes.length === 0) {
this.serverConfig.startconn();
}
}
this.on("input", function(msg, nodeSend, nodeDone) {
var payload;