Skip to content

Instantly share code, notes, and snippets.

@egm0121
Created January 31, 2013 13:48
Show Gist options
  • Save egm0121/4682989 to your computer and use it in GitHub Desktop.
Save egm0121/4682989 to your computer and use it in GitHub Desktop.
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
socket.emit('my other event', { my: 'data' });
</script>
Ciao, sono un piccolo socket.io
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(81);
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);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('bo', function (data) {
socket.broadcast.emit('news', data );
socket.emit('news', data );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment