From 0ffeb0c5afa98371b892c49a27b1f1cc313db88c Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 17 Jan 2017 20:48:05 +0000 Subject: [PATCH] Avoid creating multiple reconnect timers in websocket node --- nodes/core/io/22-websocket.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nodes/core/io/22-websocket.js b/nodes/core/io/22-websocket.js index 2e5808473..a42c3fcf9 100644 --- a/nodes/core/io/22-websocket.js +++ b/nodes/core/io/22-websocket.js @@ -36,6 +36,7 @@ module.exports = function(RED) { node.closing = false; function startconn() { // Connect to remote endpoint + node.tout = null; var socket = new ws(node.path); socket.setMaxListeners(0); node.server = socket; // keep for closing @@ -52,6 +53,7 @@ module.exports = function(RED) { if (node.isServer) { delete node._clients[id]; node.emit('closed',Object.keys(node._clients).length); } else { node.emit('closed'); } if (!node.closing && !node.isServer) { + clearTimeout(node.tout); node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? } }); @@ -61,6 +63,7 @@ module.exports = function(RED) { socket.on('error', function(err) { node.emit('erro'); if (!node.closing && !node.isServer) { + clearTimeout(node.tout); node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? } }); @@ -124,7 +127,10 @@ module.exports = function(RED) { else { node.closing = true; node.server.close(); - if (node.tout) { clearTimeout(node.tout); } + if (node.tout) { + clearTimeout(node.tout); + node.tout = null; + } } }); }