Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Created February 18, 2012 23:12
Show Gist options
  • Save ruby0x1/1861284 to your computer and use it in GitHub Desktop.
Save ruby0x1/1861284 to your computer and use it in GitHub Desktop.
socket.io + express in node.js for multiplayer games
//We will call this file server.js
var app = require('express').createServer(),
io = require('socket.io').listen(app),
url = require('url'),
pth = require('path'),
UUID = require('node-uuid');
/* Start of express/web server code. note this is just thrown here quick so you can grasp how it works. This obviously isn't good code or anything. */
var port = 3000;
//Start the express server,
//this sends the files to the clients
app.listen(port);
//This is express, too. Just an example of how to handle a request
//This takes all requests at the / of the server, and returns index.html
//from the same directory as the 'server.js' file
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html' );
});
//This is a more complex handler, allowing and disallowing
//access to certain parts of the code base and server files.
//On a remote server, you might not want to serve all files like this
//so be warned.
var shouldServeBackend = false;
app.get('/*', function(req, res, next){
//the 'file' they requested
var file = req.params[0];
//A flag to use at the end
var shouldServe = false;
if(!shouldServeBackend) {
//rubbish hack, just as an example.
//if they want a file from the media folder
//of the game, thats ok
if( file.substr(0,6) == 'media/' ) {
shouldServe = true;
}
//plus they need access to socket.io, allow it
if( file.substr(0,10) == 'socket.io/' ) {
shouldServe = true;
}
//also, if they have not added a folder, but just a filename, allow it.
if(file.indexOf('/') == -1) shouldServe = true;
} else {
//local testing can serve all files.
shouldServe = true;
}
if(shouldServe) {
//send the file to the client
res.sendfile(__dirname + '/' + file);
} else {
//print a crappy 404 message.
res.end('404 : File not found');
}
});
/* End of express/web server code */
/* Start of socket.io server code */
//This just sets up the settings of the socket.io server.
io.configure(function (){
//this is a safe for all browsers option, still realtime though.
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
//for less spam and useful information
io.set('log level', 1);
//default auth
io.set('authorization', function (handshakeData, callback) {
callback(null, true); // error first callback style
});
});
//Now we can handle connections from the client
io.sockets.on('connection', function (socket) {
//When a client connects, we send them a
//message to authorise their play session.
//Once they get this they should know the server is
//up, can show status and can do a login with
//social services, and then request a session with us
//with their database key.
var clientid = UUID();
socket.emit('_registerSession', { key:clientid } );
//on this client connection, log errors for now
socket.on('error', function(d){
console.log(d);
});
socket.on('any_game_event_you_want', function(d){
//do game code
});
});
/* End of socket.io server code */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment