Permalink
Browse files

0.0.8

  • Loading branch information...
youurayy committed May 27, 2013
1 parent a44e9a9 commit 90a30ed7ac5ea00f52103b79fa8877908859fa6e
Showing with 34 additions and 33 deletions.
  1. +1 −1 package.json
  2. +31 −31 public/ws-rpc-client.js
  3. +2 −1 public/ws-rpc-client.min.js
View
@@ -3,7 +3,7 @@
"name": "ws-rpc",
"description": "lightweight RPC support for the ws WebSocket server",
"keywords": [ "websockets", "websocket", "connect", "middleware", "express", "rpc" ],
- "version": "0.0.7",
+ "version": "0.0.8",
"homepage": "http://github.com/ypocat/ws-rpc",
"repository": {
"type": "git",
View
@@ -4,7 +4,7 @@
(function() {
function initWebSocketRPC(WebSocket) {
-
+
// caller to callee message flow:
// [1]caller.message(args, cbS) --call--> [2]callee.on('some message', client, args, cbC?)
@@ -14,7 +14,7 @@
// WebSocketRPC: create a new RPC websocket.
// *url, *protocols: see: http://dev.w3.org/html5/websockets/#the-websocket-interface
// *connTimeout: connection and data timeout in milliseconds (default: 4000, -1 to disable auto-reconnection)
-
+
var WebSocketRPC = function(url, protocols, connTimeout, reconnDelay) {
this.url = url;
@@ -27,7 +27,7 @@
try {
try { var msg = JSON.parse(e.data); }
catch(e) { throw new Error('invalid data received'); }
-
+
if(msg.r) {
var f = (this.__c || {})[msg.r];
if(!f) throw new Error('missing callback[4]:' + msg.r);
@@ -40,43 +40,43 @@
var t = this;
msg.a.push(function() {
var rmsg = { r: msg.c, a: Array.prototype.slice.call(arguments) };
-
+
if(rmsg.a.length && (e = a = rmsg.a[0]) instanceof Error) {
rmsg.a[0] = { message: e.message, stack: e.stack };
for(var p in e) a[p] = e[p];
}
-
+
t.socket.send(JSON.stringify(rmsg)); //[3]
});
}
-
+
this.emit.apply(this, msg.a); //[2]
}
catch(e) { this.emit('error', e); }
});
-
+
this.on('open', function() {
// clear the connect timeout, so that it won't disconnect us
this.__clearConnectTimeout();
});
-
+
this.on('close', function() {
// clean any pending callbacks, in a safe way
for(var ks = Object.keys(this.__c || {}), i = 0; i < ks.length; i++) {
(function(cb) {
- (process ? process.nextTick : setTimeout)(function() {
+ (typeof(process) !== 'undefined' ? process.nextTick : setTimeout)(function() {
cb(new Error('disconnected'));
}, 0);
})(this.__c[ks[i]]);
}
this.__c = null;
-
+
if(this.socket) {
for(var i = 0; i < wsEvents.length; i++) this.socket['on' + wsEvents[i]] = null;
this.socket = null;
}
-
+
// clear the connect timeout (if any), as we are not in 'connecting' phase anymore
this.__clearConnectTimeout();
// the socket was closed, so let's wait and reconnect, unless reconnection was disabled
@@ -89,7 +89,7 @@
}, t.reconnDelay);
}
});
-
+
// TEMPORARY FIX for https://github.com/einaros/ws/issues/31
this.on('error', function(e) {
@@ -102,7 +102,7 @@
// initiate the connection; this is called automatically from the constructor
-
+
WebSocketRPC.prototype.connect = function() {
var t = this;
if(this.socket)
@@ -112,7 +112,7 @@
else
// no auto-reconnect, let's continue with connecting here
this.disconnect();
-
+
// setup the establish-connection timeout
if(this.connTimeout !== -1) {
this.connTimer = setTimeout(function() {
@@ -122,29 +122,29 @@
}
}, this.connTimeout);
}
-
+
this.socket = new WebSocket(this.url, this.protocols);
for(var i = 0; i < wsEvents.length; i++)
- this.socket['on' + wsEvents[i]] =
+ this.socket['on' + wsEvents[i]] =
function(event) {
return function() { t.__dispatch(event, arguments); };
}(wsEvents[i]);
};
-
+
// disconnect from the server
// *noreconnect: disable the automatic reconnection
-
+
WebSocketRPC.prototype.disconnect = function(noreconnect) {
if(!this.socket) return;
if(noreconnect) this.connTimeout = -1;
//if(this.socket.readyState !== WebSocket.CLOSED)
try { this.socket.close(); } catch(e) { this.emit('error', e); }
};
-
+
// message: send a message to the server
-
+
WebSocketRPC.prototype.message = function() {
var last = arguments.length - 1;
var msg = {};
@@ -158,25 +158,25 @@
this.socket.send(JSON.stringify(msg)); //[1]
};
-
+
// on: register a listener
-
+
WebSocketRPC.prototype.on = function(event, listener) {
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push(listener);
};
// removeListener: remove a listener
-
+
WebSocketRPC.prototype.removeListener = function(event, listener) {
if(event in this.listeners === false) return;
this.listeners[event].splice(this.listeners[event].indexOf(fct), 1);
};
-
-
+
+
// emit: emit an event
-
+
WebSocketRPC.prototype.emit = function(event) {
if(event in this.listeners === false) return;
for(var i = 0, l = this.listeners[event], ln = l.length; i < ln; i++)
@@ -185,17 +185,17 @@
// clear the connection timer
-
+
WebSocketRPC.prototype.__clearConnectTimeout = function() {
if(this.connTimer) {
clearTimeout(this.connTimer);
delete this.connTimer;
}
};
-
-
+
+
// dispatch a WebSocket event onto WebSocketRPC
-
+
WebSocketRPC.prototype.__dispatch = function(event, args) {
var arr = Array.prototype.slice.call(args);
arr.unshift(event);
@@ -215,7 +215,7 @@
// WebSocket API events
var wsEvents = [ 'open', 'error', 'close', 'message' ];
-
+
return WebSocketRPC;
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 90a30ed

Please sign in to comment.