我正在寻找一种集成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>
在这个例子中,我必须使用fs。当我去到URL中的8080端口时,readFile不工作。
有什么建议吗?谢谢大家。
从Apache端口80提供静态内容,并通过Socket提供动态/数据内容。8080端口上的IO服务器。你不需要app = require('http').createServer(handler)
在你的插座。IO程序
Apache端口80 |-------------| 客户 |------------| 套接字。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
这在我的生产服务器上工作,运行apache端口80和NodeJS在端口8000上。用你想要的选项改变NodeJS端口…
- 为nodejs服务器的文件创建一个名为"nodejs"的文件夹,位于/var/www/html
- 在不同于80的端口上运行Nodejs,例如端口8000 执行命令:a2enmod proxy_http
- 执行a2enmod proxy_wstunnel
将接下来的两行放在以下文件的末尾:/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/$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
sudo service apache2 restart
客户端
我使用下面的库来实现Socket。在AngularJS中,但是我认为这个指南
对于socket的基本Javascript实现也很有用。输入输出技术。
调用PHP服务器:example.com
调用NodeJS服务器:example.com/nodejs
调用NodeJS Socket: example.com <——这个调用将默认由库
希望能帮到你!
您可以继续从Apache提供页面,但您需要在node.js/socket.io中启用CORS:
index.html (from Apache)
<script src = "https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
<script>
const socket = io("ws://your.domain.com:8080");
<script>
app.js (In nodejs):(启用CORS)
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
http.listen(8080);