1

解决socket.io不能使用websocket长连接,一直轮询请求的问题

 1 year ago
source link: https://blog.p2hp.com/archives/8615
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

解决socket.io不能使用websocket长连接,一直轮询请求的问题 | Lenix Blog

参考:https://socket.io/docs/v4/troubleshooting-connection-issues/#a-proxy-in-front-of-your-servers-does-not-accept-the-WebSocket-connection

可能的解释:

服务器前面的代理不接受 WebSocket连接

请在此处查看文档。

在Nginx代理上,修改nginx配置

http {
server {
listen 80;
server_name example.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
http {
  server {
    listen 80;
    server_name example.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

      proxy_pass http://localhost:3000;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }
}

如图所示: 下面不再一直出现大量的 https://xxxxxx.com:13001/socket.io/?EIO=4&transport=polling&t=O4ugyw1&sid=Uyh3dEoug6btFfvbAAliy请求.

而是出现类型为websocket的连接,时间那一列显示为待处理.说明此时已启用了websocket长连接.

2022-06-06-19-09-04%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE-1.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK