the hiscore list. •Organized using the cluster api in Node.js 0.6.X •1 parent process and X child processes handling game •If a child dies get’s restarted
workers handle the websockets for (var i = 0; i < numCPUs; i++) { var worker = cluster.fork(); worker.on('message', function(msg) { if(msg != null && msg['cmd'] == 'online') { console.log("================================ worker online"); console.dir(msg); } }); } // If the worker thread dies just print it to the console and for a new one cluster.on('death', function(worker) { console.log('worker ' + worker.pid + ' died'); cluster.fork(); });
= connectUtils.parseCookie(request.httpRequest.headers['cookie']); // Grab the session id var sessionId = cookie['connect.sid']; // Grab the username based on the session id and initialize the board state.sessionsCollection.findOne({id:sessionId}, function(err, session) { if(err) throw err; session = typeof session == 'undefined' || session == null ? {} : session; initializeBoard(state, session, self); }) •Uses connect npm package for nice to have cookie parser functions •Using capped collection aswell
{new:true, upsert:false}, function(err, board) { // Code to deal with the board or the non existance of the board } Initializing a board •Using findAndModify to pick a board •Atomic operation to add player to existing board •Process bound
Let's grab the record state.gameCollection.findOne({id: self.connectionId, state:'n'}, {raw:true}, function(err, rawDoc) { if(rawDoc) { // Retrieve the board by id from cache var boardId = state.boardIdByConnections[self.connectionId]; var board = state.connectionsByBoardId[boardId]; // Send the data to all the connections expect the originating connection for(var i = 0; i < board.length; i++) { if(board[i] != self.connectionId) { if(state.connections[board[i]] != null) state.connections[board [i]].sendBytes(rawDoc); } } } }); Binary message passing •BSON de/serialization in the browser for movement. Raw mode queries.
mind killer •If you users are in Germany have a local server for Germany •Hard to test games, time consuming •Anybody know a good test framework tell me :)