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
- WebsocketRpc.prototype.dispose = function() {
+ WebsocketRpcCaller.prototype.dispose = function() {
if (this._onMessageProxy) {
this._socket.removeListener('message', this._onMessageProxy);
delete this._onMessageProxy;
/**
* 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++) {
/**
* 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');
};
/**
* 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');
};
* 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
/**
* 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));
};
/**
* Handler for the websocket `message` event
*/
- WebsocketRpc.prototype._onMessage = function(message_raw) {
+ WebsocketRpcCaller.prototype._onMessage = function(message_raw) {
var self = this,
packet,
returnFn,
/**
* 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() {
- WebsocketRpc.prototype._noop = function() {};
+ WebsocketRpcCaller.prototype._noop = function() {};
return WebsocketRpc;
},\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
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
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
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
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 () {
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
-WebsocketRpc.prototype.dispose = function() {
+WebsocketRpcCaller.prototype.dispose = function() {
if (this._onMessageProxy) {
this._socket.removeListener('message', this._onMessageProxy);
delete this._onMessageProxy;
/**
* 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++) {
/**
* 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');
};
/**
* 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');
};
* 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
/**
* 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));
};
/**
* Handler for the websocket `message` event
*/
-WebsocketRpc.prototype._onMessage = function(message_raw) {
+WebsocketRpcCaller.prototype._onMessage = function(message_raw) {
var self = this,
packet,
returnFn,
/**
* 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() {
-WebsocketRpc.prototype._noop = function() {};
+WebsocketRpcCaller.prototype._noop = function() {};