Skip to content

Instantly share code, notes, and snippets.

@OliverF
Created January 15, 2015 18:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save OliverF/ddc88eae83675ae3aac4 to your computer and use it in GitHub Desktop.
var io = require('socket.io').listen(8441, {log: true});
var readline = require('readline');
var net = require('net');
var sleep = require('sleep');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var TURNTIME = 60;
var PILISTENPORT = 1234;
var queue = [];
var turn = null;
var piClient = null;
var listenServer = net.createServer(function(s){
console.log("Pi connected");
var lostConnection = function(){
console.log("Lost connection to Pi");
piClient = null;
};
s.on('end', lostConnection);
s.on('error', lostConnection);
piClient = s;
});
function sendToPi(data)
{
if (piClient === null)
return;
else
piClient.write(data);
}
function tick()
{
if (queue.length > 0)
{
queue[0].timeRemaining -= 1;
if (queue[0].timeRemaining < 1 || queue[0].kick === true)
{
queue[0].socket.emit('endturn');
sendToPi("stop");
console.log("Turn ended for " + queue[0].address);
queue.shift();
}
}
update();
}
function init()
{
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err.stack);
});
setInterval(tick, 1000);
listenServer.listen(PILISTENPORT, function(){
console.log("Listening for Pi connection on port " + PILISTENPORT);
});
}
function update()
{
for(i = 0; i < queue.length; i++)
{
queue[i].socket.emit('update', {positionInQueue: i, turnTimeRemaining: queue[0].timeRemaining, turnTime: TURNTIME});
}
}
io.sockets.on('connection', function(socket)
{
var address = socket.request.connection.remoteAddress;
var existingClientsWithThisAddress = queue.filter(function(client)
{
return client.address === address;
});
if (existingClientsWithThisAddress.length > 0)
{
//this client is already disconnected
console.log(address + " joined. (Already in queue, disconnecting)");
socket.emit('message', "You're already in the queue! Please wait your turn!");
socket.disconnect();
}
else
{
var client = {socket: socket, address: address, timeRemaining: TURNTIME, kick: false};
queue.push(client);
console.log(address + " joined. (Added to queue)");
}
socket.on('disconnect', function()
{
//remove from queue
for (var i = 0; i < queue.length; i++)
if (queue[i].address === address)
queue.splice(i, 1);
console.log(address + " left. (Removed from queue)");
});
socket.on('command', function(data)
{
if (!queue[0])
return;
if (address !== queue[0].address)
return;
sendToPi(data);
});
});
function processInput(input)
{
var args = input.split(" ");
if (args[0] === "quit" || args[0] === "q")
{
if (piClient !== null)
piClient.end();
rl.close();
process.exit(0);
}
else if(args[0] === "skipuser")
{
if (queue.length > 0)
{
queue[0].kick = true;
}
}
rl.question("", processInput);
}
init();
rl.question("", processInput);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment