From f66886dbdb9d453c42e4ad9f097b63ee946d6419 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 23 Oct 2015 23:01:25 +0100 Subject: [PATCH] Websocket input node not unsubscribing properly Fixes #739 --- nodes/core/io/22-websocket.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/nodes/core/io/22-websocket.js b/nodes/core/io/22-websocket.js index f58fc2348..0cf5e03ce 100644 --- a/nodes/core/io/22-websocket.js +++ b/nodes/core/io/22-websocket.js @@ -48,7 +48,7 @@ module.exports = function(RED) { if (!node.isServer) { node.emit('opened',''); } }); socket.on('close',function() { - if (node.isServer) { delete node._clients[id]; node.emit('opened',Object.keys(node._clients).length); } + if (node.isServer) { delete node._clients[id]; node.emit('closed',Object.keys(node._clients).length); } else { node.emit('closed'); } if (!node.closing && !node.isServer) { node.tout = setTimeout(function(){ startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ? @@ -126,6 +126,14 @@ module.exports = function(RED) { this._inputNodes.push(handler); } + WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) { + this._inputNodes.forEach(function(node, i, inputNodes) { + if (node === handler) { + inputNodes.splice(i, 1); + } + }); + } + WebSocketListenerNode.prototype.handleEvent = function(id,/*socket*/socket,/*String*/event,/*Object*/data,/*Object*/flags){ var msg; if (this.wholemsg) { @@ -187,6 +195,11 @@ module.exports = function(RED) { } else { this.error(RED._("websocket.errors.missing-conf")); } + + this.on('close', function() { + node.serverConfig.removeInputNode(node); + }); + } RED.nodes.registerType("websocket in",WebSocketInNode);