mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Avoid creating multiple reconnect timers in websocket node
This commit is contained in:
		@@ -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;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user