Skip to content

Instantly share code, notes, and snippets.

@whatadewitt
Created January 18, 2012 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatadewitt/1630041 to your computer and use it in GitHub Desktop.
Save whatadewitt/1630041 to your computer and use it in GitHub Desktop.
My first go at sockets
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(1337);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var t = undefined;
io.sockets.on('connection', function (socket) {
var color;
if (t == undefined) {
t=setInterval( function() {
var n = Math.floor(Math.random() * 6);
switch (n) {
case 0:
color = "#ff0000";
break;
case 1:
color = "#00ff00";
break;
case 2:
color = "#0000ff";
break;
case 3:
color = "#ffff00";
break;
case 4:
color = "#ff00ff";
break;
case 5:
color = "#00ffff";
break;
}
socket.broadcast.emit('change_color', { color: color });
}, 4000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment