diff --git a/.gitignore b/.gitignore index e4e5f6c..13b4d6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*~ \ No newline at end of file +*~ +.directory diff --git a/client/css/client.css b/client/css/client.css index 71ff2be..2699f77 100644 --- a/client/css/client.css +++ b/client/css/client.css @@ -4,8 +4,4 @@ textarea#channel_box { input#text_input { width: 90%; -} - -input#submit { - width: 5%; } \ No newline at end of file diff --git a/client/index.html b/client/index.html index 4b21892..0182015 100644 --- a/client/index.html +++ b/client/index.html @@ -9,7 +9,7 @@ - + @@ -23,6 +23,9 @@

Wiktionary - Guess the Wikipedia Article!

One player chooses a secret wikipedia article. One person is chosen to guess this article. The first player can only hint at the target, via image links. Other connected players hinder the process by flooding other unrelated images in! Connect via IP on post-it note!

+
+ Whoops! Used that one before I'm afraid. +
diff --git a/client/js/client.js b/client/js/client.js index 0df7a03..78b6418 100644 --- a/client/js/client.js +++ b/client/js/client.js @@ -11,20 +11,37 @@ socket.on('msg', function(data) { $('img#img_area').attr('src', data.msg); }); -$(document).ready(function() { - var miliseconds = 2000; - $('form#chat').submit(function(e) { - e.preventDefault(); - - socket.emit('say', {msg: $('#text_input').val()}); +var miliseconds = 2000; +var used_urls = {}; + +function submit_handler(e) { + e.preventDefault(); + $('#used_url').hide(); + + var input = $('#text_input').val(); + + if (input in used_urls) { $('#text_input').val(''); + $('#used_url').show(); + } else { + socket.emit('say', {msg: input}); $('#text_input').attr('disabled', 'disabled'); $('#text_input').val('WAIT FOR ' + miliseconds); + + used_urls[input] = true; + setTimeout(function() { $('#text_input').val(''); $('#text_input').removeAttr('disabled'); $('#text_input').focus(); }, miliseconds); + } + +} + +$(document).ready(function() { + + $('form#chat').submit(submit_handler); - }); + $('#used_url').hide(); }); diff --git a/server.js b/server.js index 7d21704..a272ef9 100644 --- a/server.js +++ b/server.js @@ -1,43 +1,50 @@ var static = require('node-static'), - http = require('http'), - util = require('util'), - socket = require('socket.io'); +http = require('http'), +util = require('util'), +socket = require('socket.io'); var webroot = "./client", - port = 8000; +port = 8000; var file = new(static.Server)(webroot, { - cache: 0 + cache: 0 }); -var app = http.createServer(function(req, res) { - req.addListener('end', function() { - file.serve(req, res, function(err, result) { - if (err) { - console.error('Error serving %s - %s', req.url, err.message); - if (err.status === 404 || err.status === 500) { - file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res); - } else { - res.writeHead(err.status, err.headers); - res.end(); - } - } else { - console.log('%s - %s', req.url, res.message); - } - }); - }); -}); - -app.listen(port); +function requestHandler(req, res) { + req.addListener('end', function() { + file.serve(req, res, function(err, result) { + if (err) { + console.error('Error serving %s - %s', req.url, err.message); + if (err.status === 404 || err.status === 500) { + file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res); + } else { + res.writeHead(err.status, err.headers); + res.end(); + } + } else { + console.log('%s - %s', req.url, result.message); + } + }); + }); +} + +var app = http.createServer(requestHandler); var io = socket.listen(app); - io.sockets.on('connection', function (socket) { - socket.emit('join', {'name': socket.id}); + socket.emit('join', {'name': socket.id}); - socket.on('say', function(data) { - socket.broadcast.emit('msg', {name: socket.id, msg: data.msg}); - socket.emit('msg', {name: socket.id, msg: data.msg}); - }); + socket.on('say', function(data) { + socket.broadcast.emit('msg', {name: socket.id, msg: data.msg}); + socket.emit('msg', {name: socket.id, msg: data.msg}); + }); }); + +//Work around a bug in socket.io - it delegates old handlers correctly +//but fails to remove them in versions of node.js. See: +// https://github.com/LearnBoost/socket.io/pull/1080 +// https://github.com/LearnBoost/socket.io/issues/987 +app.removeListener('request', requestHandler); + +app.listen(port);