Created
January 18, 2012 00:38
-
-
Save whatadewitt/1630041 to your computer and use it in GitHub Desktop.
My first go at sockets
This file contains 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 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