Skip to content

Commit

Permalink
Avoid creating multiple reconnect timers in websocket node
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Jan 17, 2017
1 parent d6f6b41 commit 0ffeb0c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion nodes/core/io/22-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ?
}
});
Expand All @@ -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 ?
}
});
Expand Down Expand Up @@ -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;
}
}
});
}
Expand Down

0 comments on commit 0ffeb0c

Please sign in to comment.