NodeJS “错误:无效的WebSocket框架:使用Socket.IO时必须清除“RSV1

wyyhbhjk  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(644)

可能是什么问题,我得到这个错误“错误:无效的WebSocket框架:RSV1必须清除”?
我使用socket.io包连接到Node.js,代码如下。

let app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http, /*options*/);

io.on('connection', function (socket) {
  console.log('a user connected');
  socket.conn.on('upgrade', ()=>{
    const upgradedTransport = socket.conn.transport.name;    
  });

socket.conn.on('upgrade', ()=>{
    const upgradedTransport = socket.conn.transport.name;    
  });
socket.on('create or join', function (room) {...});
socket.on('ready', function (room) {...});
socket.on('candidate', function (event){...});
.....
.....
socket.on('hangup', function (room) {    
    socket.leave(room);
    socket.emit('hangup', room);
   
  });
});
});

下面是我的Nginx配置,

map $http_upgrade $connection_upgrade {
default upgrade;
    '' close;
}

upstream websocket {
    server 127.0.0.1:1337;
}

server {
        server_name  mydomain.subdomain.com;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;
        underscores_in_headers on;

  location /socket.io/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:1337;
        # this magic is needed for WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        }

location / {
        proxy_pass http://127.0.0.1:8094;
        }

    listen [::]:443 ssl ipv6only=on; 
    listen 443 ssl; 
    ssl_certificate ...
    ssl_certificate_key /etc/...; 
    include /etc/...;
    ssl_dhparam /etc/...;
}

 server {
    if ($host = mydomain.subdomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen       80;
        listen       [::]:80;
        server_name  mydomain.subdomain.com;
    return 404; 

}

向 Postman 提出要求时,握手详情如下,
握手详细信息

Request URL: https://mydomain.subdomain.com/socket.io
Request Method: GET
Status Code: 101 Switching Protocols
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 3DlYbOFVRGepjPrOd92Zew==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: mydomain.subdomain.com


同样响应报头,

Server: nginx/1.14.0 (Ubuntu)
Date: Tue, 10 May 2022 12:50:41 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: FB8P5FMN8gPIsHjfRrrIoe+9+OI=

可能是什么问题,我得到这个错误“错误:无效的WebSocket框架:RSV1必须安全”?谢谢你帮我。

wecizke3

wecizke31#

您可以在请求头中看到特定WebSocket扩展的设置-消息压缩:

Sec-WebSocket-Extensions: permessage-deflate;

我认为默认情况下,WebSocket客户端会对消息进行压缩,但它接收到的 Dataframe 不包含压缩位,因此会导致:

Invalid data frame: RSV1 must be clear

很可能是服务器中没有配置相关的压缩设置。因此,当您创建WebSocket Server时,请尝试将以下选项传递给它:

let server = new Server({
    perMessageDeflate: {
        threshold: 2048, // defaults to 1024

        zlibDeflateOptions: {
        chunkSize: 8 * 1024, // defaults to 16 * 1024
        },

        zlibInflateOptions: {
        windowBits: 14, // defaults to 15
        memLevel: 7, // defaults to 8
        },

        clientNoContextTakeover: true, // defaults to negotiated value.
        serverNoContextTakeover: true, // defaults to negotiated value.
        serverMaxWindowBits: 10, // defaults to negotiated value.

        concurrencyLimit: 20, // defaults to 10
    },
});


你可以看到错误在以后会消失。

相关问题