Real time mouse activity w/ NodeJS & Sockets

Quick link to the NodeJS/websocket game I made – includes source

Thanks to Reddit for helping me test this.

Steps: Get NodeJS Get npm Get SocketIO

Create server.js Paste in: [javascript] // Simple Node & Socket server

var http = require(‘http’) , url = require(‘url’) , fs = require(‘fs’) , io = require(‘../’) , sys = require(‘sys’) , server;

server = http.createServer(function(req, res){ var path = url.parse(req.url).pathname; switch (path){ case ‘/’: res.writeHead(200, {‘Content-Type’: ‘text/html’}); res.write(‘<h1>Welcome. Try the chat example.</h1>’); res.end(); break; case ‘/json.js’: case ‘/test.html’: fs.readFile(__dirname + path, function(err, data){ if (err) return send404(res); res.writeHead(200, {‘Content-Type’: path == ‘json.js’ ? ‘text/javascript’ : ‘text/html’}) res.write(data, ‘utf8’); res.end(); }); break; default: send404(res); } }),

send404 = function(res){ res.writeHead(404); res.write(‘404’); res.end(); }; server.listen(8080);

// socket.io, I choose you var io = io.listen(server) , buffer = []; io.on(‘connection’, function(client){ client.send({ buffer: buffer }); client.broadcast({ announcement: client.sessionId + ‘ connected’ }); client.on(‘message’, function(message){ var msg = { message: [client.sessionId, message] }; buffer.push(msg); if (buffer.length > 15) buffer.shift(); client.broadcast(msg); // console.log(msg); }); client.on(‘disconnect’, function(){ client.broadcast({ announcement: client.sessionId + ‘ disconnected’ }); }); }); [/javascript]

Create test.html Paste in: [html] <!doctype html> <html> <head> Multi Mouse - How many mice can I mouse eater eat? </head> <body> <h1> You can see my mouse!! ZOMG - Video explaining why I’m doign this</h1>

Connecting...

</body> </html> [/html]

Run it with

[bash] node server.js [/bash]

Test it by visiting http://whatever:8080

Quick link to the NodeJS/websocket game I made – includes source