Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created May 25, 2011 16:57
Show Gist options
  • Save indexzero/991358 to your computer and use it in GitHub Desktop.
Save indexzero/991358 to your computer and use it in GitHub Desktop.
A simple "real-world" example of using socket.io
var socket = new io.Socket('localhost', { port: 8000 });
socket.connect();
socket.on('connect', function(client) {
});
socket.on('message', function (msg) {
console.log(msg);
});
var http = require('http'),
io = require('socket.io');
server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(
"<html><head><script src=\"socket.io/socket.io.js\"></script>" +
"<script>var socket = new io.Socket(); socket.connect();</script>" +
"</head><body></body></html>"
);
});
server.listen(8000);
// socket.io
var socket = io.listen(server);
socket.on('connection', function (client) {
client.on('message', function (msg) {
console.log('The client sent us a msg!');
});
client.on('disconnect', function () {
console.log('The client disconnected');
});
});
setInterval(function () {
socket.broadcast('The time is now ' + new Date());
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment