WebSocket -Server in Python empfängt nicht alle Nachrichten, während node.js dies tutPython

Python-Programme
Anonymous
 WebSocket -Server in Python empfängt nicht alle Nachrichten, während node.js dies tut

Post by Anonymous »

Ich führe einen WebSocket -Server hinter Nginx aus und habe zwei verschiedene Implementierungen: einen in node.js und eine in Python. Der Server von Node.js WebSocket empfängt alle Nachrichten korrekt, aber der Python -Server vermisst eine der Nachrichten .
  • Der Client öffnet WebSocket:/my-App/14515/
  • Client sendet sendet Nachricht: {"Typ": "my-App: react_api: editor", "url_kwargs": {...}}
  • Client sendet Nachricht: {"Typ" : "my-App: react_api: Problem", "url_kwargs": {...}}
  • Client öffnet einen anderen WebSocket:/Editor/14515/
Bei Verwendung node.js , beide Nachrichten (Nachrichten (

Code: Select all

my-app:react_api:editor
und my-App: react_api: Problem ) werden empfangen. Aber bei Verwendung python nur die erste Nachricht (

Code: Select all

my-app:react_api:editor
) ist protokolliert und der zweite fehlt. />

Code: Select all

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;
}
< /code>

[b]Node.js WebSocket Server (funktioniert gut) < /strong> < /h3>
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("👋 Welcome to the WebSocket server!");

ws.on('message', (message) => {
console.log(`📩 [${clientURL}] Message from ${clientIP}: ${message}`);
ws.send(`📢 Echo: ${message}`);
});

ws.on('close', (code, reason) => {
console.log(`🔴 [${clientURL}] Connection closed: Code ${code}, Reason: ${reason}`);
});

ws.on('error', (err) => {
console.error(`❌ [${clientURL}] WebSocket error: ${err.message}`);
});
});

console.log("🚀 WebSocket server running on ws://localhost:8387");
Python WebSocket-Server (fehlende zweite Nachricht) [/b]

Code: Select all

import asyncio
import websockets

async def handle_connection(websocket, path):
client_ip = websocket.remote_address[0]
client_url = path if path else "/"

print(f"✅ New connection from {client_ip}, URL: {client_url}")
await websocket.send("👋 Welcome to the WebSocket server!")

try:
async for message in websocket:
print(f"📩 [{client_url}] Message from {client_ip}: {message}")
await websocket.send(f"📢 Echo: {message}")
except websockets.exceptions.ConnectionClosed as e:
print(f"🔴 [{client_url}] Connection closed: Code {e.code}, Reason: {e.reason}")
except Exception as e:
print(f"❌ [{client_url}] WebSocket error: {str(e)}")

async def main():
async with websockets.serve(handle_connection, "localhost", 8387):
await asyncio.Future()  # Run forever

print("🚀 WebSocket server running on ws://localhost:8387")
asyncio.run(main())
< /code>

 [b] Beobachtete Protokolle < /strong> < /h3>
[h4] Node.js -Protokolle (funktioniert wie erwartet wie erwartet ) [/b] [/h4]
🚀 WebSocket server running on ws://localhost:8387
✅ New connection from ::ffff:127.0.0.1, URL: /my-app/14515/
✅ New connection from ::ffff:127.0.0.1, URL: /editor/14515/
📩 [/my-app/14515/] Message from ::ffff:127.0.0.1: {"type":"my-app:react_api:editor",...}
📩 [/my-app/14515/] Message from ::ffff:127.0.0.1: {"type":"my-app:react_api:problem",...}

Python-Protokolle (fehlende My-App: react_api: Problem )

Code: Select all

🚀 WebSocket server running on ws://localhost:8387
✅ New connection from 127.0.0.1, URL: /my-app/14515/
📩 [/my-app/14515/] Message from 127.0.0.1: {"type":"my-app:react_api:editor",...}
✅ New connection from 127.0.0.1, URL: /editor/14515/
< /code>

 [b] Was ich bisher ausprobiert habe < /strong> < /h3>

 ✅  bestätigt, dass der Client beide Nachrichten [/b] durch Anmelde vor dem Senden sendet. Stellen Sie sicher, dass es die empfängt Richtige WebSocket-Verbindung. < /li>
 ✅ [b] Versucht, Nachrichten in Python < /strong> anders zu lesen.while True:
message = await websocket.recv()
< /code>
< /li>
 ✅  Versucht, eine kleine Verzögerung hinzuzufügen (await asyncio.sleep(0.01)
) [/b] In Python nach Empfang einer Nachricht. >
Warum verpasst der Python WebSocket -Server die zweite Nachricht , während die Implementierung von Node.js einwandfrei funktioniert? Wie kann ich sicherstellen, dass Python alle Websocket -Nachrichten ordnungsgemäß empfängt und verarbeitet?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post