node.js - 如何允许使用 PeerJS 与在同一台服务器上运行的 apache 和节点 js 进行 websocket 连接?
问题描述
我目前有配置,我在端口 80 和 443 上运行 apache,在端口 8000 上运行节点 js。按照这个答案,我已启用节点响应 url https://www.example.com/node
ProxyPass /node http://localhost:8000
这适用于大多数基本的节点 js 用法,但是在使用自定义 peerJS 服务器进行 websocket 连接时使用以下代码
服务器.js
const fs = require('fs');
const { PeerServer } = require('peer');
const peerServer = PeerServer({
port: 8000,
path: '/ps',
});
peerServer.on('connection', (client) => {
console.log(client)
});
客户端.js
var peer = new Peer('user',{
host: 'www.example.com',
port: 80,
path: '/node/ps'
});
它给了我错误
Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
如果我尝试在 client.js 中使用端口 443,它会显示:
Error during WebSocket handshake: Unexpected response code: 503
我的虚拟主机文件是:
<VirtualHost *:443>
ServerAdmin www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateKeyFile /path/to/.key
SSLCertificateFile /path/to/crt/file
SSLCertificateChainFile /path/to/ca/bundle
RewriteEngine on
RewriteCond %{REQUEST_URI} ^node [NC]
RewriteRule .* "wss:/localhost:8000/$1" [P,L]
ProxyPass /node https://localhost:8000/
ProxyPassReverse /node https://localhost:8000/
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Header set Access-Control-Allow-Origin "*"
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
</VirtualHost>
解决方案
推荐阅读
- reactjs - 当状态元素为对象时,避免子组件重新渲染
- excel - 如何计算不包括周末和节假日的天数,但开始日/结束日可以是周末/节假日?
- javascript - 如何通过 JavaScript 获取 html 文件?
- html - 如何垂直对齐标签中的项目?
- markdown - Gitit:Markdown 代码块语法未呈现
- python - 如何为 CEF 应用程序编写 Selenium 测试
- google-cloud-platform - 使用 Google Cloud Platform 和 AI Platform 提供预测服务
- prestashop - Prestashop 1.6 - 我的产品如何从我的模块的 php 页面购物
- angular - 创建一个可观察对象链并在所有对象完成后返回一个可观察对象
- db2 - 如何将 DB2 DDL 和 DML 审计日志编写为系统应用程序日志?