From 0fe7c000ebf3274c2aed85428f2382f0e3da7e6f Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 27 Aug 2013 21:04:55 +0100 Subject: [PATCH] WebsocketRpc dispose() update; client disconnect fix --- client/assets/libs/websocketrpc.js | 105 +---------------------------- server/client.js | 1 + server/httphandler.js | 1 - server/weblistener.js | 22 +----- server/websocketrpc.js | 4 +- 5 files changed, 9 insertions(+), 124 deletions(-) diff --git a/client/assets/libs/websocketrpc.js b/client/assets/libs/websocketrpc.js index 11d0c07..e0a49f0 100644 --- a/client/assets/libs/websocketrpc.js +++ b/client/assets/libs/websocketrpc.js @@ -9,9 +9,6 @@ function WebsocketRpc(eio_socket) { this._callbacks = {}; this._socket = eio_socket; - this._root_namespace = this; - this._namespaces = new Object(null); - this._mixinEmitter(); this._bindSocketListeners(); } @@ -31,20 +28,14 @@ WebsocketRpc.prototype._bindSocketListeners = function() { WebsocketRpc.prototype.dispose = function() { if (this._onMessageProxy) { - this._socket.off('message', this._onMessageProxy); + this._socket.removeListener('message', this._onMessageProxy); delete this._onMessageProxy; } - this.disposeNamespaces(); + this.removeAllListeners(); }; -WebsocketRpc.prototype.disposeNamespaces = function() { - for (var namespace in this._namespaces) { - this._namespaces[namespace].dispose(); - this._namespaces[namespace] = null; - } -}; /** @@ -78,21 +69,6 @@ WebsocketRpc.prototype._isResponse = function(packet) { -WebsocketRpc.prototype.namespace = function(namespace) { - // Does this namespace already exist? - if (this._namespaces[namespace]) { - return this._namespaces[namespace]; - } - - var ns = new WebsocketRpcNamespace(this, namespace); - ns._root_namespace = this._root_namespace; -console.log('Created namespace', namespace); - this._namespaces[namespace] = ns; - - return ns; -}; - - /** * Make an RPC call * First argument must be the method name to call @@ -173,89 +149,14 @@ WebsocketRpc.prototype._onMessage = function(message_raw) { } else { returnFn = function noop(){}; } -console.log(packet.method); - this.emit.apply(this, [packet.method, returnFn].concat(packet.params)); - // Forward the call on to any matching namespaces - this._forwardCall(packet, returnFn); - } -}; - - -/** - * Take a call and forward it on to any matching namespaces - */ -WebsocketRpc.prototype._forwardCall = function(packet, returnFn) { - var ns; - - for (var namespace in this._namespaces) { - ns = this._namespaces[namespace]; - - // If the method name is in this namespace, strip the namespace string off - // and emit the remaining method on the namespace object - // (namespace.api.method_name() becomes method_name()) - if (packet.method.indexOf(namespace) === 0) { - ns.emit.apply(ns, [packet.method.replace(namespace + '.', ''), returnFn].concat(packet.params)); - ns._forwardCall(packet, returnFn); - } + this.emit.apply(this, [packet.method, returnFn].concat(packet.params)); } }; - -function WebsocketRpcNamespace(parent_rpc, new_namespace) { - var self = this, - to_bind, i; - - this._namespaces = new Object(null); - - this._parent = parent_rpc; - this._namespace = new_namespace; - - if (this._namespace.substr(-1) !== '.') - this._namespace += '.'; - - // Proxy these functions from _parent, appending our namespace in the first argument - to_bind = ['namespace', 'on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners']; - for (i=0; i