Permalink
Browse files

deferreds

  • Loading branch information...
1 parent 7bc7f22 commit 36bd1ce2c4ca81ff308f2612f2d517a408eb8140 @monteslu committed Apr 4, 2012
Showing with 108 additions and 95 deletions.
  1. +45 −39 public/javascripts/rpc.js
  2. +63 −56 server.js
View
@@ -1,42 +1,49 @@
- dojo.require('dojox.rpc.Service');
- dojo.require('dojox.rpc.JsonRPC');
-
- var socket = io.connect('/');
+dojo.require('dojox.rpc.Service');
+dojo.require('dojox.rpc.JsonRPC');
+
+var socket = io.connect('/');
var wsRpc = {
callNum: 0,
- resultCBs: {},
- errorCBs: {},
- call : function(name,params,resultCB,errorCB){
+ deferreds: {},
+ call : function(name){
this.callNum++;
+ var params = [];
+ if(arguments.length > 1){
+ for(var i = 1; i < arguments.length; i++){
+ params.push(arguments[i]);
+ }
+ }
var rpcObj = {method:name,params:params,id:this.callNum};
//console.log('rpcObj for call',rpcObj);
- if(resultCB){
- this.resultCBs[this.callNum] = resultCB;
- }
- if(errorCB){
- this.errorCBs[this.callNum] = errorCB;
- }
- socket.emit('rpc',rpcObj);
+
+ var deferred = new dojo.Deferred();
+ deferred.callNum = this.callNum;
+ this.deferreds[this.callNum] = deferred;
+ socket.emit('rpc',rpcObj);
+ return deferred;
}
};
socket.on('rpc',function(rpcObj){
//console.log('rpcObj',rpcObj);
try{
if(rpcObj.error){
- if(wsRpc.errorCBs[rpcObj.id]){
+ if(wsRpc.deferreds[rpcObj.id]){
var error = rpcObj.error;
- wsRpc.errorCBs[rpcObj.id](error);
+ wsRpc.deferreds[rpcObj.id].errback(error);
}
}else{
- if(wsRpc.resultCBs[rpcObj.id]){
+ if(wsRpc.deferreds[rpcObj.id]){
var result = rpcObj.result;
- wsRpc.resultCBs[rpcObj.id](result);
- }
+ wsRpc.deferreds[rpcObj.id].callback(result);
+ }
+ }
+ if(wsRpc.deferreds[rpcObj.id]){
+ delete wsRpc.deferreds[rpcObj.id];
}
}catch(e){
console.log('malformed rpc response', rpcObj,e);
@@ -49,30 +56,29 @@ dojo.ready(function(){
dojo.connect(dojo.byId('wbutton'),'onclick',function(){
var timeStart = Date.now();
- wsRpc.call('square',dojo.byId('num1').value,
- function(result){
- dojo.byId('result').innerHTML = result;
- showElapsed(timeStart);
- },
- function(error){
- dojo.byId('result').innerHTML = 'ERROR:' + error;
- showElapsed(timeStart);
- }
- );
+ var aDeff = wsRpc.call('square',dojo.byId('num1').value);
+ aDeff.addCallback(function(result){
+ dojo.byId('result').innerHTML = result;
+ showElapsed(timeStart);
+ });
+ aDeff.addErrback(function(result){
+ dojo.byId('result').innerHTML = 'ERROR:' + error;
+ showElapsed(timeStart);
+ });
});
dojo.connect(dojo.byId('abutton'),'onclick',function(){
- var timeStart = Date.now();
- var aDeff = ajaxRpc.square(dojo.byId('num1').value);
- aDeff.addCallback(function(result){
- dojo.byId('result').innerHTML = result;
- showElapsed(timeStart);
- });
- aDeff.addErrback(function(result){
- dojo.byId('result').innerHTML = 'ERROR:' + error;
- showElapsed(timeStart);
- });
+ var timeStart = Date.now();
+ var aDeff = ajaxRpc.square(dojo.byId('num1').value);
+ aDeff.addCallback(function(result){
+ dojo.byId('result').innerHTML = result;
+ showElapsed(timeStart);
+ });
+ aDeff.addErrback(function(result){
+ dojo.byId('result').innerHTML = 'ERROR:' + error;
+ showElapsed(timeStart);
+ });
});
View
119 server.js
@@ -33,73 +33,78 @@ app.configure('production', function(){
app.get('/', routes.index);
-var jsonRpc = function(req,resp){
- if (req.method == 'POST') {
- var body = '';
- req.on('data', function (data) {
- body += data;
- });
- req.on('end', function () {
-
- try{
- rpc = JSON.parse(body);
- console.log('rpc',rpc);
- rpcFunctions[rpc.method](rpc.params,
- function(result){
- resp.send({result:result,error:null,id:rpc.id});
- },
- function(error){
- resp.send({result:null,error:error,id:rpc.id});
- });
- }catch(e){
- resp.send({result:null,error:e,id:0});
-
- }
-
- });
- }
- else{
-
-
- }
-
-
-};
app.get('/jsonrpc', function(req,resp){
- jsonRpc(req,resp);
+ jsonRpc(req.query.rpc,resp);
});
app.post('/jsonrpc', function(req,resp){
- jsonRpc(req,resp);
+ var body = '';
+ req.on('data', function (data) {
+ body += data;
+ });
+ req.on('end', function () {
+ jsonRpc(body,resp);
+ });
+
});
+
+
+var jsonRpc = function(rpcStr,resp){
+ try{
+ console.log('rpcStr: ', rpcStr);
+ rpc = JSON.parse(rpcStr);
+ console.log('rpc: ', rpc, typeof rpc);
+ var params = [];
+ params[0] = function(result){
+ resp.send({result: result, error: null, id: rpc.id});
+ };
+ params[1] = function(error){
+ resp.send({result: null, error: error, id: rpc.id});
+ };
+ if(rpc.params && rpc.params.length){
+ for(var i = 0; i < rpc.params.length; i++){
+ console.log('adding param: ', i, rpc.params[i]);
+ params.push(rpc.params[i]);
+ }
+ }
+ rpcFunctions[rpc.method].apply(rpcFunctions[rpc.method],params);
+ }catch(e){
+ resp.send({result:null,error:e,id:0});
+
+ }
+
+};
+
+
var rpcFunctions = {
- getStuff: function(args,resultCB, errCB){
+ getStuff: function(resultCB, errCB){
resultCB('stuff');
},
- getMoreStuff: function(args,resultCB,errCB){
+ getMoreStuff: function(resultCB,errCB){
resultCB({moar:'stuff!'});
},
- add: function(args,resultCB,errCB){
+ add: function(resultCB,errCB,num1,num2){
try{
- resultCB(args[0] + args[1]);
+ console.log('add num1:',num1, ' num2:', num2)
+ resultCB(num1 + num2);
}catch(e){
errCB(e);
}
},
- divide: function(args,resultCB,errCB){
+ divide: function(resultCB,errCB,dividend,divisor){
try{
- resultCB(args[0] / args[1]);
+ resultCB(dividend / divisor);
}catch(e){
errCB(e);
}
},
- square: function( args, resultCB, errCB){
+ square: function(resultCB, errCB, num){
try{
- resultCB(args * args);
+ resultCB(num * num);
}catch(e){
errCB(e);
}
@@ -137,34 +142,36 @@ console.log("Express server listening on port %d in %s mode", app.address().port
io.sockets.on('connection', function (socket) {
-
-
socket.on('rpc',function(rpc){
- console.log('rpc',rpc);
+ console.log('rpc obj: ',rpc);
if(rpcFunctions[rpc.method]){
var retVal;
var error;
try{
- rpcFunctions[rpc.method](rpc.params,
- function(result){
- socket.emit('rpc',{result: result, error: null, id: rpc.id});
- },
- function(error){
- socket.emit('rpc',{result: null, error: error, id: rpc.id});
- });
+ var params = [];
+ params[0] = function(result){
+ socket.emit('rpc',{result: result, error: null, id: rpc.id});
+ };
+ params[1] = function(error){
+ socket.emit('rpc',{result: null, error: error, id: rpc.id});
+ };
+ if(rpc.params && rpc.params.length){
+ for(var i = 0; i < rpc.params.length; i++){
+ console.log('adding param: ', i, rpc.params[i]);
+ params.push(rpc.params[i]);
+ }
+ }
+ rpcFunctions[rpc.method].apply(rpcFunctions[rpc.method],params);
}catch(e){
- socket.emit('rpc',{result: null, error: e, id: rpc.id});
+ socket.emit('rpc',{result: null, error: e, id: rpc.id});
}
-
}else{
socket.emit('rpc',{result: null, error: 'method undefined', id: rpc.id});
}
});
-
-
});

0 comments on commit 36bd1ce

Please sign in to comment.