我正在寻找一种方法来以下列方式集成Node.js + Socket.io + Apache: 我希望apache继续提供HTML/JS文件。 我想Node.js的监听端口连接8080事情是这样的:Node.js + Socket.io + Apache
var util = require("util"),
app = require('http').createServer(handler),
io = require('/socket.io').listen(app),
fs = require('fs'),
os = require('os'),
url = require('url');
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.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
socket.emit('ok 1', { hello: 'world' });
});
socket.on('clientMSG', function (data) {
socket.emit('ok 2', { hello: 'world' });
});
});
如果我访问连接到该服务器上的HTML,它的工作原理,但我需要去mydomian.com:8080 /index.html。 我想要的是能够去mydomian.com/index.html。并能够打开一个套接字连接:
<script>
var socket = io.connect('http://mydomain.com', {port: 8080});
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data from the client' });
});
socket.on('connect', function (data) {
console.log("connect");
});
socket.on('disconnect', function (data) {
console.log("disconnect");
});
//call this function when a button is clicked
function sendMSG()
{
console.log("sendMSG");
socket.emit('clientMSG', { msg: 'non-scheduled message from client' });
}
</script>
在这个例子中,我不得不使用不会工作fs.readFile当我去到端口8080的URL。
有什么建议吗?韩国社交协会。
尝试nginx而不是apache,特别是如果你只是想提供静态文件。 Apache为每个请求启动一个新线程,这与使用节点的理念/原因有部分相反。 – badunk