Node.js + Socket.io + Apache

我正在寻找一种方法来整合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' }); }); }); 

如果我访问连接到这个服务器的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工作。

有什么build议么? 韩国社交协会。

从Apache端口80提供静态内容,并通过端口8080上的Socket.IO服务器提供dynamic/数据内容app = require('http').createServer(handler)您的Socket.IO应用程序中不需要app = require('http').createServer(handler)

Apache port 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'); }); }); 

AWS + APACHE + NODEJS + SOCKET.IO + ANGULARJS

服务器端
这对我运行在端口80上的Apache和端口8000上的NodeJS的生产服务器上。更改NodeJS端口的期望选项…

  1. 在/ var / www / html中为NodeJS服务器的文件创build一个名为“nodejs”的文件夹
  2. 在不同于80的端口上运行Nodejs,例如端口8000
  3. 执行命令:a2enmod proxy_http
  4. 执行命令:a2enmod proxy_wstunnel
  5. 把下面的两行放到以下文件的末尾:/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 
  6. 将接下来的12行放在以下文件末尾:/sites-available/000-default.conf
    (如果你有一个不同的网站创build你的线,那里)

     RewriteEngine On RewriteCond %{REQUEST_URI} ^socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule /{.*} ws://localhost:8000/$1 [P,L] RewriteCond %{HTTP:Connection} Upgrade [NC] RewriteRule /(.*) ws://localhost:8000/$1 [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 
  7. sudo服务apache2重启

客户端
我使用下面的库在AngularJS中实现Socket.io,但我认为这个指南
对于socket.io技术的基本的Javascript实现也是有用的。

打电话给我的PHP服务器:example.com
要调用NodeJS服务器:example.com/nodejs
调用NodeJS Socket:example.com <—这个调用默认由库来完成

我希望能帮到你!