My kids are obsessed with building an online "game" in which characters move around a screen and interact with others on the screen (mostly chatting). I have no real intention of building a MMORPG for them, but perhaps I can use my current exploration of fab.js to help out the kids.
Ultimately, I will give them a
<canvas>
element on which they can move their character around and watch others move their characters around (again, no intention of doing this for real, so I will not bother to support Internet Explorer). First up, I need to report location to a fab.js server so that it can broadcast this information to all connected listeners.As usual, I start with a skeleton fab.js app:
var puts = require( "sys" ).puts;I will take a small step tonight and try to gather and report a single location:
with ( require( ".." ) )
( fab )
( listen, 0xFAB )
( 404 );
var puts = require( "sys" ).puts;To actually get the location information, say from query parameters passed into the URL, I need to return a listener to the downstream (fab) app which expects to be passed the header information:
with ( require( ".." ) )
( fab )
( listen, 0xFAB )
( /^\/location/ )
(
function() {
var out = this;
out({body: "location info here"});
}
)
( 404 );
(Once I send the downstream app the location info, I tell it that I am done by calling it with no arguments. This is all very basic fab.js stuff. But how to process the header information?
function() {
var out = this;
return function( head ) {
// process header information
var app = out({ body: "location info here" });
if ( app ) app();
};
}
)
The query parameters are easily accessed from the
head.url.search
property, which then needs a bit of parsing. Thankfully I do not have to roll my own, there is the query string module from node.js:(Accessing that with curl, I get:
function() {
var out = this;
return function( head ) {
var search = head.url.search.substring(1);
var q = require('querystring').parse(search);
var app = out({ body: "x: " +q.x });
if ( app ) app();
};
}
)
curl http://localhost:4011/location?x=1 -iNice!
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked
x: 1
I would just as soon reply with JSON. Rather than building my own inside strings, I build a Javascript object then rely on fab.stringify to convert that to text:
( stringify )Now when I access via curl I get a nice JSON response:
(
function() {
var out = this;
return function( head ) {
var search = head.url.search.substring(1);
var q = require('querystring').parse(search);
var app = out({ body: {x: q.x } });
if ( app ) app();
};
}
)
cstrom@whitefall:~/repos/fab$ curl http://localhost:4011/location?x=1 -iI finish off by handling the y-coordinate and some defaults:
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked
{"x":1}
( stringify )That will do for tonight. I will move onto broadcasting coordinates tomorrow. Fortunately, the fab.js code includes a very simple broadcast example. Unfortunately for me, this will likely involve me try to figure out what happens when you
(
function() {
var out = this;
return function( head ) {
var search = head.url.search.substring(1);
var q = require('querystring').parse(search);
var app = out({ body: {x: q.x || 0, y: q.y || 0} });
if ( app ) app();
};
}
)
call()
on an array, supplying a function for the execution context:// from fab/examples/broadcast.jsMind: blown.
function broadcast( listeners ) {
listeners.call( function( obj ){ listeners = obj.body } );
Day #87
No comments:
Post a Comment