javascript Node.js + Socket.io + Apache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7682338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js + Socket.io + Apache
提问by oscarm
I'm looking for a way to integrate Node.js + Socket.io + Apache in the following way: I want apache to continue serving HTML / JS files. I want node.js to listen for connection on port 8080. Something like this:
我正在寻找一种通过以下方式集成 Node.js + Socket.io + Apache 的方法:我希望 apache 继续提供 HTML/JS 文件。我想让 node.js 监听 8080 端口上的连接。像这样:
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' });
});
});
if I access a HTML that connect to this server, it works, but I need to go to mydomian.com:8080/index.html. What I want is to be able to go to mydomian.com/index.html. and be able to open a socket connection:
如果我访问连接到此服务器的 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>
In this example I had to use fs.readFile of wont work when I go to the port 8080 in the URL.
在这个例子中,当我转到 URL 中的端口 8080 时,我不得不使用 fs.readFile 不起作用。
Any suggestions? Tks.
有什么建议?tks。
回答by EhevuTov
Serve your static content from Apache port 80 and serve your dynamic/data content over a Socket.IO server on port 8080. You don't need the app = require('http').createServer(handler)
in your Socket.IO app
从 Apache 端口 80 提供您的静态内容,并通过端口 8080 上的 Socket.IO 服务器提供您的动态/数据内容app = require('http').createServer(handler)
。您的 Socket.IO 应用程序中不需要
Apache port 80 |-------------| clients |------------| Socket.IO port 8080
Apache 80 端口 |-------------| 客户|------------| Socket.IO 端口 8080
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
io.sockets.emit('this', { will: 'be received by everyone'});
socket.on('clientMSG', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
sockets.emit('user disconnected');
});
});
回答by Philip Enc
AWS + APACHE + NODEJS + SOCKET.IO + ANGULARJS
AWS + APACHE + NODEJS + SOCKET.IO + ANGULARJS
SERVER SIDE
This worked for me on a production server running apache on port 80 and NodeJS
on port 8000. Change the NodeJS port by your desired option…
服务器端
这对我在端口 80 上运行 apache 和端口 8000 上运行 NodeJS 的生产服务器上工作。通过您想要的选项更改 NodeJS 端口......
- Create a folder named “nodejs” for the files of the NodeJS server at /var/www/html
- Run Nodejs on a different port than 80, for example port 8000
- Execute command: a2enmod proxy_http
- Execute command: a2enmod proxy_wstunnel
Put the next 2 lines at the end of the following file: /etc/apache2/apache2.conf
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
Put the next 12 lines at the end of the following file: /sites-available/000-default.conf
(If you have a different site created by you, put the lines there)RewriteEngine On RewriteCond %{REQUEST_URI} ^socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule /{.*} ws://localhost:8000/ [P,L] RewriteCond %{HTTP:Connection} Upgrade [NC] RewriteRule /(.*) ws://localhost:8000/ [P,L] ProxyPass /nodejs http://localhost:8000/ ProxyPassReverse /nodejs http://localhost:8000/ ProxyPass /socket.io http://localhost:8000/socket.io ProxyPassReverse /socket.io http://loacalhost:8000/socket.io ProxyPass /socket.io ws://localhost:8000/socket.io ProxyPassReverse /socket.io ws://localhost:8000/socket.io
sudo service apache2 restart
- 在 /var/www/html 为 NodeJS 服务器的文件创建一个名为“nodejs”的文件夹
- 在与 80 不同的端口上运行 Nodejs,例如端口 8000
- 执行命令:a2enmod proxy_http
- 执行命令:a2enmod proxy_wstunnel
将接下来的 2 行放在以下文件的末尾:/etc/apache2/apache2.conf
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
将接下来的 12 行放在以下文件的末尾:/sites-available/000-default.conf
(如果您创建了不同的站点,请将这些行放在那里)RewriteEngine On RewriteCond %{REQUEST_URI} ^socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule /{.*} ws://localhost:8000/ [P,L] RewriteCond %{HTTP:Connection} Upgrade [NC] RewriteRule /(.*) ws://localhost:8000/ [P,L] ProxyPass /nodejs http://localhost:8000/ ProxyPassReverse /nodejs http://localhost:8000/ ProxyPass /socket.io http://localhost:8000/socket.io ProxyPassReverse /socket.io http://loacalhost:8000/socket.io ProxyPass /socket.io ws://localhost:8000/socket.io ProxyPassReverse /socket.io ws://localhost:8000/socket.io
须藤服务 apache2 重启
CLIENT SIDE
I use the following libraryto implement Socket.io in AngularJS, but
I think this guide
is useful too for a basic Javascript implementation of socket.io technology.
客户端
我使用以下库在 AngularJS 中实现 Socket.io,但我认为本指南
对于 socket.io 技术的基本 Javascript 实现也很有用。
To call my PHP server: example.com
To call NodeJS server: example.com/nodejs
To call NodeJS Socket: example.com <---this call will be done by default by the library
调用我的 PHP 服务器:example.com
调用 NodeJS 服务器:example.com/nodejs
调用 NodeJS Socket:example.com <---这个调用将由库默认完成
I hope help you!
希望能帮到你!