Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.0.8

  • Loading branch information...
commit 90a30ed7ac5ea00f52103b79fa8877908859fa6e 1 parent a44e9a9
@youurayy authored
View
2  package.json
@@ -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
62 public/ws-rpc-client.js
@@ -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,9 +158,9 @@
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);
@@ -168,15 +168,15 @@
// 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;
};
View
3  public/ws-rpc-client.min.js
@@ -1 +1,2 @@
-!function(){function initWebSocketRPC(WebSocket){var WebSocketRPC=function(url,protocols,connTimeout,reconnDelay){this.url=url;this.protocols=protocols;this.connTimeout=connTimeout||4e3;this.listeners={};this.reconnDelay=reconnDelay||1e3;this.on("message",function(e){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);delete this.__c[msg.r];f.apply(null,msg.a);return}if(msg.c){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))})}this.emit.apply(this,msg.a)}catch(e){this.emit("error",e)}});this.on("open",function(){this.__clearConnectTimeout()});this.on("close",function(){for(var ks=Object.keys(this.__c||{}),i=0;i<ks.length;i++){!function(cb){(process?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}this.__clearConnectTimeout();if(this.connTimeout!==-1){var t=this;setTimeout(function(){if(this.connTimeout!==-1)t.connect()},t.reconnDelay)}});this.on("error",function(e){if(typeof module!=="undefined"&&e.message&&e.message.indexOf("ECONNREFUSED")!=-1)this.emit("close")});this.connect()};WebSocketRPC.prototype.connect=function(){var t=this;if(this.socket)if(this.connTimeout!==-1)return this.disconnect();else this.disconnect();if(this.connTimeout!==-1){this.connTimer=setTimeout(function(){if(t.socket.readyState!==WebSocket.OPEN){t.__clearConnectTimeout();t.disconnect()}},this.connTimeout)}this.socket=new WebSocket(this.url,this.protocols);for(var i=0;i<wsEvents.length;i++)this.socket["on"+wsEvents[i]]=function(event){return function(){t.__dispatch(event,arguments)}}(wsEvents[i])};WebSocketRPC.prototype.disconnect=function(noreconnect){if(!this.socket)return;if(noreconnect)this.connTimeout=-1;try{this.socket.close()}catch(e){this.emit("error",e)}};WebSocketRPC.prototype.message=function(){var last=arguments.length-1;var msg={};if(typeof arguments[last]==="function"){if(!this.__c)this.__c={};do{msg.c=randomId()}while(this.__c[msg.c]);this.__c[msg.c]=arguments[last--]}msg.a=Array.prototype.slice.call(arguments,0,last+1);this.socket.send(JSON.stringify(msg))};WebSocketRPC.prototype.on=function(event,listener){this.listeners[event]=this.listeners[event]||[];this.listeners[event].push(listener)};WebSocketRPC.prototype.removeListener=function(event,listener){if(event in this.listeners===false)return;this.listeners[event].splice(this.listeners[event].indexOf(fct),1)};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++)l[i].apply(this,Array.prototype.slice.call(arguments,1))};WebSocketRPC.prototype.__clearConnectTimeout=function(){if(this.connTimer){clearTimeout(this.connTimer);delete this.connTimer}};WebSocketRPC.prototype.__dispatch=function(event,args){var arr=Array.prototype.slice.call(args);arr.unshift(event);this.emit.apply(this,arr)};function randomId(){for(var id="",i=0,f=Math.floor,r=Math.random,s=String.fromCharCode;i<8;i++)id+=s(f(r()*26)+(f(r()*2)?97:65));return id}var wsEvents=["open","error","close","message"];return WebSocketRPC}if(typeof module!=="undefined")module.exports=initWebSocketRPC;else window.InitWebSocketRPC=initWebSocketRPC}();
+// @ypocat 2012, bsd lic
+(function(){function b(b){function d(){for(var a="",b=0,c=Math.floor,d=Math.random,e=String.fromCharCode;b<8;b++)a+=e(c(d()*26)+(c(d()*2)?97:65));return a}var c=function(b,c,d,f){this.url=b,this.protocols=c,this.connTimeout=d||4e3,this.listeners={},this.reconnDelay=f||1e3,this.on("message",function(b){try{try{var c=JSON.parse(b.data)}catch(b){throw new Error("invalid data received")}if(c.r){var d=(this.__c||{})[c.r];if(!d)throw new Error("missing callback[4]:"+c.r);delete this.__c[c.r],d.apply(null,c.a);return}if(c.c){var e=this;c.a.push(function(){var d={r:c.c,a:Array.prototype.slice.call(arguments)};if(d.a.length&&(b=a=d.a[0])instanceof Error){d.a[0]={message:b.message,stack:b.stack};for(var f in b)a[f]=b[f]}e.socket.send(JSON.stringify(d))})}this.emit.apply(this,c.a)}catch(b){this.emit("error",b)}}),this.on("open",function(){this.__clearConnectTimeout()}),this.on("close",function(){for(var a=Object.keys(this.__c||{}),b=0;b<a.length;b++)(function(a){(typeof process!="undefined"?process.nextTick:setTimeout)(function(){a(new Error("disconnected"))},0)})(this.__c[a[b]]);this.__c=null;if(this.socket){for(var b=0;b<e.length;b++)this.socket["on"+e[b]]=null;this.socket=null}this.__clearConnectTimeout();if(this.connTimeout!==-1){var c=this;setTimeout(function(){this.connTimeout!==-1&&c.connect()},c.reconnDelay)}}),this.on("error",function(a){typeof module!="undefined"&&a.message&&a.message.indexOf("ECONNREFUSED")!=-1&&this.emit("close")}),this.connect()};c.prototype.connect=function(){var a=this;if(this.socket){if(this.connTimeout!==-1)return this.disconnect();this.disconnect()}this.connTimeout!==-1&&(this.connTimer=setTimeout(function(){a.socket.readyState!==b.OPEN&&(a.__clearConnectTimeout(),a.disconnect())},this.connTimeout)),this.socket=new b(this.url,this.protocols);for(var c=0;c<e.length;c++)this.socket["on"+e[c]]=function(b){return function(){a.__dispatch(b,arguments)}}(e[c])},c.prototype.disconnect=function(a){if(!this.socket)return;a&&(this.connTimeout=-1);try{this.socket.close()}catch(b){this.emit("error",b)}},c.prototype.message=function(){var a=arguments.length-1,b={};if(typeof arguments[a]=="function"){this.__c||(this.__c={});do b.c=d();while(this.__c[b.c]);this.__c[b.c]=arguments[a--]}b.a=Array.prototype.slice.call(arguments,0,a+1),this.socket.send(JSON.stringify(b))},c.prototype.on=function(a,b){this.listeners[a]=this.listeners[a]||[],this.listeners[a].push(b)},c.prototype.removeListener=function(a,b){if(a in this.listeners==0)return;this.listeners[a].splice(this.listeners[a].indexOf(fct),1)},c.prototype.emit=function(a){if(a in this.listeners==0)return;for(var b=0,c=this.listeners[a],d=c.length;b<d;b++)c[b].apply(this,Array.prototype.slice.call(arguments,1))},c.prototype.__clearConnectTimeout=function(){this.connTimer&&(clearTimeout(this.connTimer),delete this.connTimer)},c.prototype.__dispatch=function(a,b){var c=Array.prototype.slice.call(b);c.unshift(a),this.emit.apply(this,c)};var e=["open","error","close","message"];return c}typeof module!="undefined"?module.exports=b:window.InitWebSocketRPC=b})();
Please sign in to comment.
Something went wrong with that request. Please try again.