Skip to content

Instantly share code, notes, and snippets.

@DTrejo
Created October 10, 2010 04:55
Show Gist options
  • Save DTrejo/618969 to your computer and use it in GitHub Desktop.
Save DTrejo/618969 to your computer and use it in GitHub Desktop.
var sys = require('sys')
, url = require('url')
, http = require('http')
, eyes = require('eyes')
, querystring = require('querystring')
, io = require(__dirname + '/lib/socket.io-node')
, PORT = 80 // MAKE SURE THIS IS SAME AS SOCKET.IO
, static = require('node-static')
// Create a node-static server to serve the current directory
, fileServer = new(static.Server)('./public', { cache: 7200, headers: {'X-Hello':'World!'} })
, httpServer = http.createServer(function (request, response) {
request.body = '';
request.addListener('data',function(chunk){
request.body += chunk;
});
request.addListener('end', function () {
var params = querystring.parse(url.parse(request.url).query);
var posted = querystring.parse(request.body);
sys.puts(request.body);
if (request.url.search('/digits.xml') > -1) {
// do shitt with key data :)
// serve digits.xml
fileServer.serveFile('/digits.xml', 200, { 'Content-Type':'text/xml' } , request, response);
}
else if (request.url.search('/controller.xml') > -1) {
// serve controller.xml
fileServer.serveFile('/controller.xml', 200, { 'Content-Type':'text/xml' } , request, response);
}
else {
if (request.url == '/') {
request.url = "index.html";
}
fileServer.serve(request, response, function (err, res) {
if (err) { // An error as occured
sys.error("> Error serving " + request.url + " - " + err.message);
response.writeHead(err.status, err.headers);
response.end();
} else { // The file was served successfully
sys.puts("> " + request.url + " - " + res.message);
}
});
}
});
}).listen(PORT)
, hi = eyes.inspect(httpServer);
// , io = io.listen(httpServer);
console.log('> client is listening on http://127.0.0.1:' + PORT);
// socket.io, sample chat app.
// var io = io.listen(httpServer);
io.on('connection', function(client) {
// test data for Justin.
client.send({ newPlayer: '555-500-1337' });
client.send({ action: '9' });
// tell other clients they joined
client.broadcast({ newPlayer: client.sessionId });
client.on('message', function(message){
var action = { action: [client.sessionId, message] };
// if (players.length > 15) players.shift(); // this makes players drop when greater than 15
// tell others what key you clicked.
client.broadcast(action);
});
client.on('disconnect', function(){
client.broadcast({ dropPlayer: client.sessionId });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment