Created
May 25, 2011 16:57
-
-
Save indexzero/991358 to your computer and use it in GitHub Desktop.
A simple "real-world" example of using socket.io
This file contains hidden or 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 socket = new io.Socket('localhost', { port: 8000 }); | |
socket.connect(); | |
socket.on('connect', function(client) { | |
}); | |
socket.on('message', function (msg) { | |
console.log(msg); | |
}); |
This file contains hidden or 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'), | |
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