HTML and CSS Reference
In-Depth Information
Listing 11-14. GRITS's Directional Input Socket for Android
var controller_endpoint = '/'+game_id+'!'+id;
console.log('STARTING TO LISTEN ON WASD CHANNEL', controller_endpoint);
io.of(controller_endpoint).on('connection', function(wasdSocket) {
ref = {};
console.log('CONTROLLER CONNECTED TO WASD CHANNEL', controller_endpoint);
wasdSocket.on('disconnect', function(msg) {
console.log('RECEIVED DISCONNECT FOR WASD CHANNEL', controller_endpoint);
});
wasdSocket.on('message', function(msg) {
// console.log('GOT WASD CONTROLLER MESSAGE', msg,
// 'ON WASD CHANNEL', controller_endpoint);
wasdSocket.send('ack ' + msg);
if (msg.slice(0,4) == 'init') {
ref = JSON.parse(msg.slice(4));
return;
}
if (!ref['player_name']) {
console.log('disconnecting', controller_endpoint,
'wasd controller; received msg', msg, 'while ref has no player_name:', ref);
wasdSocket.disconnect();
return;
}
W = (msg[0] == 'Y');
A = (msg[1] == 'Y');
S = (msg[2] == 'Y');
D = (msg[3] == 'Y');
socket.q_wasd({from: ref['player_name'].slice(1), W: W, A: A, S: S, D: D});
});
});
Readers are encouraged to watch the Google IO talk by Colt McAnlis on GRITS at https://developers.google.
com/events/io/2012/sessions/gooio2012/210 . The previous snippet comes from https://code.google.com/p/
gritsgame/source/browse/src/games-server/main.js .
Rawkets
Rawkets is a proof-of-concept, online, multiplayer game developed by Rob Hawkes, the head of developer relations
at Pusher and former technical evangelist at Mozilla. The game explores HTML5 Canvas, WebGL, and WebSockets.
Rawkets is reminiscent of the classic arcade game Asteroids. A simple line-art ship represents the players; they can
move about the world and shoot at other players.
You can peruse the Rawkets code at https://github.com/robhawkes/rawkets . Rawkets uses PHP for some of its
code. PHP is outside the scope of this chapter, so I will leave it as an exercise for the reader to work out how to install a
LAMP (Linux, Apache, MySQL, PHP [or Perl]) server and get the code running.
Rawkets uses many of the techniques described earlier in this chapter. The interesting portions of code lie in the
main.js file at https://github.com/robhawkes/rawkets/blob/422026f0fcd31db7645281568cf5b6d1d7668932/
server/main.js .
Listing 11-15 provides all the specific message types in Rawkets. A unit of data for a Rawkets game takes this
general form.
 
Search WWH ::




Custom Search