我开始学习socket.io并使用this example of chat。
当我转到ip:8080/public/index.html
时,我还需要访问其他文件,例如其他JS脚本,这些文件将在浏览器的客户端使用。但是当我把脚本加载像这样:
<script src="/js/phaser.js" type="text/javascript"></script>
Web服务器不会返回它,我需要在此处理程序代码上。
我有这段代码:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(8080);
function handler (req, res) {
console.log(req.headers.referer);
fs.readFile(__dirname + '/public/index.html', // <--- I need here put filename which client wants it, but when I console.log to req it return HUGE data, I not found anythink usefull
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
socket.broadcast.emit('new message', data);
console.log(data);
});
socket.on('msg', function(data){
console.log(data);
})
});