Created
February 19, 2013 23:20
-
-
Save octaflop/4991187 to your computer and use it in GitHub Desktop.
A lot of room for improvement, but this is the engine powering: https://legionofevil.org and it uses nginx + websockets + ssl without any patches! here's the nginx conf: https://gist.github.com/octaflop/4991052
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var app = express(); | |
| var server = require('http').createServer(app); | |
| var io = require("socket.io").listen(server); | |
| var redis = require('redis'), | |
| db = redis.createClient(); | |
| // Socket.io configuration | |
| io.configure(function () { | |
| io.enable('browser client etag'); | |
| io.set('transports', [ | |
| 'websocket', 'xhr-polling' | |
| //'xhr-polling' // for benchmarking | |
| ]); | |
| }); | |
| // Docpad instance for static files | |
| var docpadInstanceConfiguration = { | |
| serverExpress: app, | |
| serverHttp: server, | |
| middlewareStandard: false | |
| }; | |
| var docpadInstance = require('docpad').createInstance(docpadInstanceConfiguration, function(err) { | |
| if (err) return console.log(err.stack); | |
| docpad.action('generate server watch', function(err){ | |
| if (err) return console.log(err.stack); | |
| }); | |
| }); | |
| server.listen(3000); | |
| app.enable('trust proxy'); // for nginx/express proxy magic | |
| app.use(function(err, req, res, next) { | |
| console.error(err.stack); | |
| res.send(500, "Oops."); | |
| }); | |
| var players = []; | |
| var nextId = 0; | |
| // Game server part | |
| io.sockets.on('connection', function(socket) { | |
| var player; | |
| socket.on('logon', function(pos) { | |
| // Create the player | |
| player = { id: nextId++, x: pos.x, y: pos.y }; | |
| // Send existing players to client | |
| socket.emit('players', players); | |
| // Send the new player to other clients | |
| socket.broadcast.emit('connected', player); | |
| // Add client to list of players | |
| players.push(player); | |
| }); | |
| socket.on('move', function(data) { | |
| if (player) { | |
| player.x = data.x; | |
| player.y = data.y; | |
| // Broadcast position change to all other clients | |
| socket.broadcast.emit('moved', player); | |
| } | |
| }); | |
| socket.on('disconnect', function() { | |
| players.splice(players.indexOf(player), 1); | |
| io.sockets.emit('disconnected', player); | |
| }); | |
| socket.on('message', function(msg){ | |
| socket.send(msg); // For latency checking | |
| }); | |
| }); | |
| app.get("/", function (req, res) { | |
| // res.send(req.online.length + ' users online'); | |
| res.sendfile(__dirname + "/dist" + "/index.html"); | |
| }); | |
| app.get("/about/", function (req, res) { | |
| // res.send(req.online.length + ' users online'); | |
| res.sendfile(__dirname + "/out" + "/about.html"); | |
| }); | |
| app.get("/blog/", function (req, res) { | |
| // res.send(req.online.length + ' users online'); | |
| res.sendfile(__dirname + "/out/blog" + "/index.html"); | |
| }); | |
| var usernames = {}; | |
| app.use(express.logger()); | |
| app.use('/blog', express.static(__dirname + '/out/blog')); | |
| app.use('/', express.static(__dirname + '/dist')); | |
| console.log('listening on port 3000'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment