Created
August 8, 2012 16:29
-
-
Save heapwolf/3296405 to your computer and use it in GitHub Desktop.
socket.io/server
This file contains 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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Private gists would be better imho :)