From: Darren Date: Tue, 27 Aug 2013 20:04:55 +0000 (+0100) Subject: WebsocketRpc dispose() update; client disconnect fix X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0fe7c000ebf3274c2aed85428f2382f0e3da7e6f;p=KiwiIRC.git WebsocketRpc dispose() update; client disconnect fix --- 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