var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
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.on('sendchat', function (data) {
io.sockets.emit('updatechat', data);
});
});
This is my websocket server (node.js + socket.io). I understand everything except the handler function. Could someone please explain what it does? And what does index.html do, and where is it located? On my client side im using a razor view named something completly different, and it works anyway.
Thanks
Your handler()
function is called when a new request comes in. It allows you to serve other resources from the same HTTP server Socket.IO is using.
index.html
is just a sample file that you can use to test your pages with.