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

Avoid creating multiple reconnect timers in websocket node

This commit is contained in:
Nick O'Leary 2017-01-17 20:48:05 +00:00
parent d6f6b41145
commit 0ffeb0c5af
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -36,6 +36,7 @@ module.exports = function(RED) {
node.closing = false; node.closing = false;
function startconn() { // Connect to remote endpoint function startconn() { // Connect to remote endpoint
node.tout = null;
var socket = new ws(node.path); var socket = new ws(node.path);
socket.setMaxListeners(0); socket.setMaxListeners(0);
node.server = socket; // keep for closing 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); } if (node.isServer) { delete node._clients[id]; node.emit('closed',Object.keys(node._clients).length); }
else { node.emit('closed'); } else { node.emit('closed'); }
if (!node.closing && !node.isServer) { 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() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
} }
}); });
@ -61,6 +63,7 @@ module.exports = function(RED) {
socket.on('error', function(err) { socket.on('error', function(err) {
node.emit('erro'); node.emit('erro');
if (!node.closing && !node.isServer) { 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() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
} }
}); });
@ -124,7 +127,10 @@ module.exports = function(RED) {
else { else {
node.closing = true; node.closing = true;
node.server.close(); node.server.close();
if (node.tout) { clearTimeout(node.tout); } if (node.tout) {
clearTimeout(node.tout);
node.tout = null;
}
} }
}); });
} }