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);