다음과 같은 방법으로 Node.js + Socket.io + 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>
내가 URL에 포트 8080에 갈 때 늘 작업의 fs.readFile를 사용했다이 예에서 :와 소켓 연결을 열 수 있습니다.
제안 사항? Tks.
정적 파일을 제공하려는 경우 특히 아파치 대신 nginx를 사용하십시오. Apache는 노드를 사용함에 대한 철학/이유에 부분적으로 비견되는 모든 요청에 대해 새 스레드를 시작합니다. – badunk