Created
August 14, 2013 07:50
-
-
Save youweit/6228819 to your computer and use it in GitHub Desktop.
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 http = require('http'), | |
fs = require('fs'), | |
// NEVER use a Sync function except at start-up! | |
index = fs.readFileSync(__dirname + '/index.html'); | |
// Send index.html to all requests | |
var app = http.createServer(function(req, res) { | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end(index); | |
}); | |
// Socket.io server listens to our app | |
var io = require('socket.io').listen(app); | |
// Send current time to all connected clients | |
function sendTime() { | |
io.sockets.emit('time', { time: new Date().toJSON() }); | |
} | |
// Send current time to all connected clients | |
function sendPerson() { | |
io.sockets.emit('person', { person: "Alan" }); | |
} | |
// Send current time every 10 secs | |
setInterval(sendTime, 10000); | |
setInterval(sendPerson, 1000); | |
// Emit welcome message on connection | |
io.sockets.on('connection', function(socket) { | |
socket.emit('welcome', { message: 'Welcome!' }); | |
socket.on('i am client', console.log); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment