admin管理员组

文章数量:1277896

I am running a WebSocket server behind Nginx, and I have two different implementations: one in Node.js and one in Python. The Node.js WebSocket server receives all messages correctly, but the Python server misses one of the messages.

WebSocket Message Flow:

  1. Client opens WebSocket: /my-app/14515/
  2. Client sends message: { "type": "my-app:react_api:editor", "url_kwargs": {...} }
  3. Client sends message: { "type": "my-app:react_api:problem", "url_kwargs": {...} }
  4. Client opens another WebSocket: /editor/14515/

When using Node.js, both messages (my-app:react_api:editor and my-app:react_api:problem) are received. But when using Python, only the first message (my-app:react_api:editor) is logged, and the second one is missing.


Nginx Configuration:

location /ws/ {
    proxy_pass http://127.0.0.1:8387/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;
    proxy_buffering off;
    proxy_cache off;
    proxy_read_timeout 3600s;
    proxy_send_timeout 3600s;
    include proxy_params;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Node.js WebSocket Server (Works Fine)

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8387 });

wss.on('connection', (ws, req) => {
    const clientIP = req.socket.remoteAddress;
    const clientURL = req.url || '/'; 

    console.log(`✅ New connection from ${clientIP}, URL: ${clientURL}`);

    ws.send("

本文标签: WebSocket Server in Python Not Receiving All Messages While Nodejs DoesStack Overflow