WebsocketRpc: Use instance as a function instead of confusing .call function
authorDarren <darren@darrenwhitlen.com>
Fri, 19 Sep 2014 11:02:35 +0000 (12:02 +0100)
committerDarren <darren@darrenwhitlen.com>
Fri, 19 Sep 2014 11:02:35 +0000 (12:02 +0100)
client/assets/libs/engine.io.tools.js
client/src/app.js
client/src/models/gateway.js
server/client.js
server/websocketrpc.js

index e24ba2449d7ea42921a6b095e93e798cd805a180..8758d79b9873e7c54e8faa5d9c407f4b54806ec0 100644 (file)
@@ -107,19 +107,35 @@ var EngineioTools = {
             Some way to expire unused callbacks? TTL? expireCallback() function?
         */
 
+        /**
+         * Wrapper around creating a new WebsocketRpcCaller
+         * This lets us use the WebsocketRpc object as a function
+         */
         function WebsocketRpc(eio_socket) {
-            var self = this;
+            var caller = new WebsocketRpcCaller(eio_socket);
+            var ret = function WebsocketRpcInstance() {
+                return ret.makeCall.apply(ret, arguments);
+            };
+
+            for(var prop in caller){
+                ret[prop] = caller[prop];
+            }
 
+            ret._mixinEmitter();
+            ret._bindSocketListeners();
+
+            return ret;
+        }
+
+
+        function WebsocketRpcCaller(eio_socket) {
             this._next_id = 0;
             this._rpc_callbacks = {};
             this._socket = eio_socket;
-
-            this._mixinEmitter();
-            this._bindSocketListeners();
         }
 
 
-        WebsocketRpc.prototype._bindSocketListeners = function() {
+        WebsocketRpcCaller.prototype._bindSocketListeners = function() {
             var self = this;
 
             // Proxy the onMessage listener
@@ -131,7 +147,7 @@ var EngineioTools = {
 
 
 
-        WebsocketRpc.prototype.dispose = function() {
+        WebsocketRpcCaller.prototype.dispose = function() {
             if (this._onMessageProxy) {
                 this._socket.removeListener('message', this._onMessageProxy);
                 delete this._onMessageProxy;
@@ -146,7 +162,7 @@ var EngineioTools = {
         /**
          * The engine.io socket already has an emitter mixin so steal it from there
          */
-        WebsocketRpc.prototype._mixinEmitter = function() {
+        WebsocketRpcCaller.prototype._mixinEmitter = function() {
             var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners'];
 
             for (var i=0; i<funcs.length; i++) {
@@ -159,7 +175,7 @@ var EngineioTools = {
         /**
          * Check if a packet is a valid RPC call
          */
-        WebsocketRpc.prototype._isCall = function(packet) {
+        WebsocketRpcCaller.prototype._isCall = function(packet) {
             return (typeof packet.method !== 'undefined' &&
                     typeof packet.params !== 'undefined');
         };
@@ -168,7 +184,7 @@ var EngineioTools = {
         /**
          * Check if a packet is a valid RPC response
          */
-        WebsocketRpc.prototype._isResponse = function(packet) {
+        WebsocketRpcCaller.prototype._isResponse = function(packet) {
             return (typeof packet.id !== 'undefined' &&
                     typeof packet.response !== 'undefined');
         };
@@ -180,9 +196,9 @@ var EngineioTools = {
          * First argument must be the method name to call
          * If the last argument is a function, it is used as a callback
          * All other arguments are passed to the RPC method
-         * Eg. Rpc.call('namespace.method_name', 1, 2, 3, callbackFn)
+         * Eg. Rpc.makeCall('namespace.method_name', 1, 2, 3, callbackFn)
          */
-        WebsocketRpc.prototype.call = function(method) {
+        WebsocketRpcCaller.prototype.makeCall = function(method) {
             var params, callback, packet;
 
             // Get a normal array of passed in arguments
@@ -213,7 +229,7 @@ var EngineioTools = {
         /**
          * Encode the packet into JSON and send it over the websocket
          */
-        WebsocketRpc.prototype.send = function(packet) {
+        WebsocketRpcCaller.prototype.send = function(packet) {
             if (this._socket)
                 this._socket.send(JSON.stringify(packet));
         };
@@ -222,7 +238,7 @@ var EngineioTools = {
         /**
          * Handler for the websocket `message` event
          */
-        WebsocketRpc.prototype._onMessage = function(message_raw) {
+        WebsocketRpcCaller.prototype._onMessage = function(message_raw) {
             var self = this,
                 packet,
                 returnFn,
@@ -263,7 +279,7 @@ var EngineioTools = {
         /**
          * Returns a function used as a callback when responding to a call
          */
-        WebsocketRpc.prototype._createReturnCallFn = function(packet_id) {
+        WebsocketRpcCaller.prototype._createReturnCallFn = function(packet_id) {
             var self = this;
 
             return function returnCallFn() {
@@ -280,7 +296,7 @@ var EngineioTools = {
 
 
 
-        WebsocketRpc.prototype._noop = function() {};
+        WebsocketRpcCaller.prototype._noop = function() {};
 
 
         return WebsocketRpc;
index e7d643d626c28f6ac4e901e4d4070a9ce52ddf09..f83ee8375d49771f2a837aa9470abd1784cd5be2 100644 (file)
@@ -33,7 +33,11 @@ _kiwi.global = {
     },\r
 \r
     rpc: function() {\r
-        _kiwi.gateway.rpc.call.call(_kiwi.gateway.rpc, arguments);\r
+        if (!_kiwi.gateway.rpc) {\r
+            throw 'RPC unavailable. Is Kiwi connected to the server yet?';\r
+        }\r
+\r
+        _kiwi.gateway.rpc.apply(_kiwi.gateway.rpc, arguments);\r
     },\r
 \r
     addMediaMessageType: function(match, buildHtml) {\r
index c130d01dbdae9f4ae326c9e46a2af191f8052bfa..a8d8f1af3fbdf72892c662bf440814a952db8bfa 100644 (file)
@@ -169,7 +169,7 @@ _kiwi.model.Gateway = Backbone.Model.extend({
         if (connection_info.options.encoding)\r
             server_info.encoding = connection_info.options.encoding;\r
 \r
-        this.rpc.call('kiwi.connect_irc', server_info, function (err, server_num) {\r
+        this.rpc('kiwi.connect_irc', server_info, function (err, server_num) {\r
             if (!err) {\r
                 callback_fn && callback_fn(err, server_num);\r
 \r
@@ -196,7 +196,7 @@ _kiwi.model.Gateway = Backbone.Model.extend({
             args = {\r
                 build_version: _kiwi.global.build_version\r
             };\r
-            this.rpc.call('kiwi.client_info', args);\r
+            this.rpc('kiwi.client_info', args);\r
 \r
             this.connect_callback && this.connect_callback();\r
             delete this.connect_callback;\r
@@ -244,13 +244,18 @@ _kiwi.model.Gateway = Backbone.Model.extend({
         this.trigger('connection:' + command, data);\r
     },\r
 \r
+    /**\r
+    *   Make an RPC call with the connection_id as the first argument\r
+    *   @param  {String}    method          RPC method name\r
+    *   @param  {Number}    connection_id   Connection ID this call relates to\r
+    */\r
     rpcCall: function(method, connection_id) {\r
         var args = Array.prototype.slice.call(arguments, 0);\r
 \r
         if (typeof args[1] === 'undefined' || args[1] === null)\r
             args[1] = _kiwi.app.connections.active_connection.get('connection_id');\r
 \r
-        return this.rpc.call.apply(this.rpc, args);\r
+        return this.rpc.apply(this.rpc, args);\r
     },\r
 \r
     /**\r
index 0a88fbfaf2861c8dba8fe103698d20e6476b7bd6..724b51cefa2517e5e851d4587ad5fd98537332be 100755 (executable)
@@ -76,12 +76,12 @@ module.exports.Client = Client;
 
 Client.prototype.sendIrcCommand = function (command, data, callback) {
     var c = {command: command, data: data};
-    this.rpc.call('irc', c, callback);
+    this.rpc('irc', c, callback);
 };
 
 Client.prototype.sendKiwiCommand = function (command, data, callback) {
     var c = {command: command, data: data};
-    this.rpc.call('kiwi', c, callback);
+    this.rpc('kiwi', c, callback);
 };
 
 Client.prototype.dispose = function () {
index 5f7c56e7a7bacaa3d10cb93242baaa1670aca468..1bc8cf52769973b48a82fe3ec6117e5ec429b763 100644 (file)
@@ -4,19 +4,35 @@
     Some way to expire unused callbacks? TTL? expireCallback() function?
 */
 
+/**
+ * Wrapper around creating a new WebsocketRpcCaller
+ * This lets us use the WebsocketRpc object as a function
+ */
 function WebsocketRpc(eio_socket) {
-    var self = this;
+    var caller = new WebsocketRpcCaller(eio_socket);
+    var ret = function WebsocketRpcInstance() {
+        return ret.makeCall.apply(ret, arguments);
+    };
+
+    for(var prop in caller){
+        ret[prop] = caller[prop];
+    }
 
+    ret._mixinEmitter();
+    ret._bindSocketListeners();
+
+    return ret;
+}
+
+
+function WebsocketRpcCaller(eio_socket) {
     this._next_id = 0;
     this._rpc_callbacks = {};
     this._socket = eio_socket;
-
-    this._mixinEmitter();
-    this._bindSocketListeners();
 }
 
 
-WebsocketRpc.prototype._bindSocketListeners = function() {
+WebsocketRpcCaller.prototype._bindSocketListeners = function() {
     var self = this;
 
     // Proxy the onMessage listener
@@ -28,7 +44,7 @@ WebsocketRpc.prototype._bindSocketListeners = function() {
 
 
 
-WebsocketRpc.prototype.dispose = function() {
+WebsocketRpcCaller.prototype.dispose = function() {
     if (this._onMessageProxy) {
         this._socket.removeListener('message', this._onMessageProxy);
         delete this._onMessageProxy;
@@ -43,7 +59,7 @@ WebsocketRpc.prototype.dispose = function() {
 /**
  * The engine.io socket already has an emitter mixin so steal it from there
  */
-WebsocketRpc.prototype._mixinEmitter = function() {
+WebsocketRpcCaller.prototype._mixinEmitter = function() {
     var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners'];
 
     for (var i=0; i<funcs.length; i++) {
@@ -56,7 +72,7 @@ WebsocketRpc.prototype._mixinEmitter = function() {
 /**
  * Check if a packet is a valid RPC call
  */
-WebsocketRpc.prototype._isCall = function(packet) {
+WebsocketRpcCaller.prototype._isCall = function(packet) {
     return (typeof packet.method !== 'undefined' &&
             typeof packet.params !== 'undefined');
 };
@@ -65,7 +81,7 @@ WebsocketRpc.prototype._isCall = function(packet) {
 /**
  * Check if a packet is a valid RPC response
  */
-WebsocketRpc.prototype._isResponse = function(packet) {
+WebsocketRpcCaller.prototype._isResponse = function(packet) {
     return (typeof packet.id !== 'undefined' &&
             typeof packet.response !== 'undefined');
 };
@@ -77,9 +93,9 @@ WebsocketRpc.prototype._isResponse = function(packet) {
  * First argument must be the method name to call
  * If the last argument is a function, it is used as a callback
  * All other arguments are passed to the RPC method
- * Eg. Rpc.call('namespace.method_name', 1, 2, 3, callbackFn)
+ * Eg. Rpc.makeCall('namespace.method_name', 1, 2, 3, callbackFn)
  */
-WebsocketRpc.prototype.call = function(method) {
+WebsocketRpcCaller.prototype.makeCall = function(method) {
     var params, callback, packet;
 
     // Get a normal array of passed in arguments
@@ -110,7 +126,7 @@ WebsocketRpc.prototype.call = function(method) {
 /**
  * Encode the packet into JSON and send it over the websocket
  */
-WebsocketRpc.prototype.send = function(packet) {
+WebsocketRpcCaller.prototype.send = function(packet) {
     if (this._socket)
         this._socket.send(JSON.stringify(packet));
 };
@@ -119,7 +135,7 @@ WebsocketRpc.prototype.send = function(packet) {
 /**
  * Handler for the websocket `message` event
  */
-WebsocketRpc.prototype._onMessage = function(message_raw) {
+WebsocketRpcCaller.prototype._onMessage = function(message_raw) {
     var self = this,
         packet,
         returnFn,
@@ -161,7 +177,7 @@ WebsocketRpc.prototype._onMessage = function(message_raw) {
 /**
  * Returns a function used as a callback when responding to a call
  */
-WebsocketRpc.prototype._createReturnCallFn = function(packet_id) {
+WebsocketRpcCaller.prototype._createReturnCallFn = function(packet_id) {
     var self = this;
 
     return function returnCallFn() {
@@ -178,7 +194,7 @@ WebsocketRpc.prototype._createReturnCallFn = function(packet_id) {
 
 
 
-WebsocketRpc.prototype._noop = function() {};
+WebsocketRpcCaller.prototype._noop = function() {};