Created
September 19, 2011 13:22
-
-
Save cjstewart88/1226475 to your computer and use it in GitHub Desktop.
server
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 io = require('socket.io'); | |
var redis = require("redis"); | |
var url = require("url"); | |
var server = express.createServer(); | |
var port = process.env.PORT || 3000; | |
server.configure(function() { | |
server.set('view engine', 'html'); | |
server.set('views', __dirname + '/views'); | |
server.set('view options', {layout: false}); | |
server.register('.html', { | |
compile: function(str, options){ | |
return function(locals){ | |
return str; | |
}; | |
} | |
}); | |
server.use(express.static(__dirname + '/public')); | |
}); | |
// routes | |
server.get('/', function(req, res) { | |
res.render('index'); | |
}); | |
server.listen(port); | |
/* REDIS SHIT */ | |
if (process.env.REDISTOGO_URL) { | |
var rtg = url.parse(process.env.REDISTOGO_URL); | |
var redisClient = redis.createClient(rtg.port, rtg.hostname); | |
redisClient.auth(rtg.auth.split(":")[1]); | |
} else { | |
var redisClient = redis.createClient(); | |
} | |
redisClient.on("connect", function () { | |
redisClient.flushall(); // This eventually needs to be removed, if the server goes idle and starts back up it flushes the redis data store | |
}); | |
redisClient.on("error", function (err) { | |
console.log("Error " + err); | |
}); | |
/* SOCKET.IO SHIT */ | |
var io = io.listen(server); | |
// heroku said to do this socket.io config shit so I did it, not sure what it does | |
io.configure(function () { | |
io.set('transports', ['xhr-polling']); | |
io.set('polling duration', 10); | |
}); | |
// handler for when a new client connects | |
io.sockets.on('connection', function (client) { | |
// setup new clients stuff | |
var clientId = client['id']; | |
// add the new client to the 'clients' set in redis | |
// one day this will check to see if the client that connected is already a client | |
// and basically just "sign them in" and mark them connected | |
redisClient.sadd("clients", "client:"+clientId); | |
redisClient.hmset("client:"+clientId, "clientId", clientId, "status", "connected", "x", 0, "y", 0); | |
// get the list of currently connected clients and also let already connected | |
// clients know about the new user | |
redisClient.smembers("clients", function (err, clients) { | |
console.log(clients); | |
var initClientList = {}; | |
var clientCount = 0; | |
for (var thisClientId in clients) { | |
redisClient.hgetall(clients[thisClientId], function (err, clientData) { | |
if (clientData["status"] == "connected") { | |
initClientList[clientData["clientId"]] = [parseInt(clientData["x"]), parseInt(clientData["y"])]; | |
} | |
clientCount++ | |
// we only need to send our messages when all the currently connected clients are added to the 'initClientList' | |
if (clientCount == clients.length) { | |
// send the init action to get the newly connected client started | |
client.emit('init', { clientId: clientId, initClientList: initClientList }); | |
// let the already connected clients know about the newly connected client | |
client.broadcast.emit('clientConnected', { clientId: clientId, x: 0, y: 0 }); | |
// we dont need this anymore after its been sent | |
initClientList = null; | |
} | |
}); | |
} | |
}); | |
// clients requesting to move | |
client.on('requestToMoveClient', function (data) { | |
var direction = data.direction; | |
redisClient.hgetall("client:"+clientId, function (err, client) { | |
var x = parseInt(client["x"]); | |
var y = parseInt(client["y"]); | |
if (direction == "left" && x-100 >= 0) redisClient.hincrby("client:"+clientId, "x", -100, function() { clientMoved(); }); | |
else if (direction == "right" && x+100 <= 4000) redisClient.hincrby("client:"+clientId, "x", 100, function() { clientMoved(); }); | |
else if (direction == "up" && y-100 >= 0) redisClient.hincrby("client:"+clientId, "y", -100, function() { clientMoved(); }); | |
else if (direction == "down" && y+100 <= 3000) redisClient.hincrby("client:"+clientId, "y", 100, function() { clientMoved(); }); | |
}); | |
function clientMoved () { | |
client.broadcast.emit('clientMoved', { clientId: clientId, direction: direction }); | |
client.emit('clientMoved', { clientId: clientId, direction: direction }); | |
} | |
}); | |
// client disconnected | |
client.on('disconnect', function () { | |
redisClient.hset("client:"+clientId, "status", "disconnected"); | |
client.broadcast.emit('clientDisconnected', { clientId: clientId }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment