Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created August 8, 2012 16:29
Show Gist options
  • Save heapwolf/3296405 to your computer and use it in GitHub Desktop.
Save heapwolf/3296405 to your computer and use it in GitHub Desktop.
socket.io/server
var fs = require('fs');
var server = require('http').createServer(handler);
var io = require('socket.io').listen(server);
function handler (req, res) {
if (req.url === '/') {
res.statusCode = 200;
res.setHeader('content-type', 'text/html');
fs.createReadStream('./index.html').pipe(res);
}
}
io.sockets.on('connection', function (socket) {
socket.on('echo', function (data) {
io.sockets.emit('echo', data, socket.username);
});
socket.on('setName', function (name) {
socket.username = name;
io.sockets.emit('join', name);
});
});
server.listen(8080);
@dscape
Copy link

dscape commented Aug 8, 2012

Private gists would be better imho :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment