Tuesday, December 6, 2011

Server Side functions

So lets say we have a situation where we have a game where most of the player variables are stored server side, and only showed or used by the player as a "stats" page.  The actual usage of the stats, lets say for calculating the attack power or something to that effect would all take place server side, because it's quicker that way.  So how do we make these 'remote procedure calls' ?

A Node.js module called dnode: dnode-protocol github repo.


Here is what it is. This is the server.js:
var dnode = require('dnode');

var server = dnode({
    mul : function (n, m, cb) { cb(n * m) }
});
server.listen(5050);
And here is the client.js:
var dnode = require('dnode');

dnode.connect(5050, function (remote) {
    remote.mul(10, 20, function (n) {
        console.log('10 * 20 = ' + n);
    });
});
Now when you run client.js, you get the output:
$ node client.js
200

So the potential for this is great.  Computing most things server side will greatly free up client side operations.

No comments:

Post a Comment