连接时的Socket Io错误(WebSocket握手过程中的错误:意外的响应代码:400)

时间:2018-12-15 01:55:44

标签: javascript node.js apache websocket socket.io

我在Socket.io的客户端/服务器端有问题,我有一个VPS,最近我使用了“ Certbot”来自动在我的域上启用HTTPS。 好吧,我在debian jessie上使用apache,并使用了Certbot启用了https的url这样的“ api.domaine.com”,我在Express和Socket.io服务器端使用Node.js。

我目前正在使用Socket扩展chrome扩展,而io是客户端。

这是服务器上的io配置:

let PORT = process.env.PORT || 1337;
let server = app.listen(PORT);
let io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket) {
  socket.on('joinRoom', function(room) {
    socket.join(room);
    console.log(io.sockets.adapter.rooms);
    socket.emit('joined', room);
  });
});

我在background.js中来自客户端的连接线:

let socket = io.connect('https://api.domain.com/');

我的问题是浏览器的控制台(Chrome开发工具)返回了以下内容:

WebSocket connection to 'wss://api.domain.com/socket.io/?EIO=3&transport=websocket&sid=T4dxwvDQUX8PvS86AAAF' failed: Error during WebSocket handshake: Unexpected response code: 400

编辑:我的SSL虚拟主机配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>
   ServerName api.domain.com

   ProxyPass / http://0.0.0.0:1337/
   ProxyPassReverse / http://0.0.0.0:1337/
   ProxyPreserveHost On
SSLCertificateFile /etc/letsencrypt/live/api.domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

1 个答案:

答案 0 :(得分:0)

我通过进行一些更改来解决它,并在我域的Apache Virtualhost SSL配置中添加了代理配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>
   ServerName api.domain.com

   SSLEngine on
   SSLProxyEngine On
   ProxyRequests Off

   ProxyPass / http://0.0.0.0:1337/
   ProxyPassReverse / http://0.0.0.0:1337/
   ProxyPreserveHost On

   SSLCertificateFile /etc/letsencrypt/live/api.domain.com/fullchain.pem
   SSLCertificateKeyFile /etc/letsencrypt/live/api.domain.com/privkey.pem
   Include /etc/letsencrypt/options-ssl-apache.conf

   RewriteEngine on
   RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
   RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
   RewriteRule .* ws://0.0.0.0:1337%{REQUEST_URI} [P]
</VirtualHost>
</IfModule>