Skip to content

Instantly share code, notes, and snippets.

@sreekumar-kr
Created July 30, 2012 06:54
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 sreekumar-kr/3205400 to your computer and use it in GitHub Desktop.
Save sreekumar-kr/3205400 to your computer and use it in GitHub Desktop.
nodejs realtime http server
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
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 users = 0;
io.sockets.on('connection', function (socket) {
users++;
console.log("user ++");
io.sockets.emit('countChanged', {count:users });
socket.on('disconnect', function (socket) {
users--;
console.log("user --");
io.sockets.emit('countChanged', {count:users});
});
socket.on('typing', function (data) {
io.sockets.emit('textChanged', {val:data.text});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment