首页 > 解决方案 > 如何允许使用 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>

标签: node.jsapachewebsocketproxypeerjs

解决方案


推荐阅读