miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.36k stars 890 forks source link

Error 400 when upgrading to websocket transport #1863

Closed DaRealCodeWritten closed 2 years ago

DaRealCodeWritten commented 2 years ago

Describe the bug Flask-SocketsIO + Flask, attempts to use websockets as transport results in a 400 response code and a traceback on the client side, using simple-websocket as the WS framework and NGINX as the reverse proxy Nginx config is as follows

server {
    server_name czvr-bot.xyz;
    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://czvr-bot.xyz;
    }

    # SSL (if wanted)
    listen 443 ssl;
    ssl_certificate fullchain1.pem;
    ssl_certificate_key privkey1.pem;
}

Expected behavior Website upgrades to WSS for the connection between the site and the bot

Actual behavior Client throws the below traceback and the website refuses the upgrade

Logs Python-SocketIO client (A discord bot)

Traceback (most recent call last):
  File "C:\Users\Eric Lysne\PycharmProjects\VancouverSite\bot.py", line 318, in inner
    await sock.connect("https://czvr-bot.xyz", transports="websocket")
  File "C:\Users\Eric Lysne\PycharmProjects\VancouverSite\venv\lib\site-packages\socketio\asyncio_client.py", line 149, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: Connection error

Flask-SocketIO server (A flask app)

Invalid websocket upgrade
INFO:engineio.server:Invalid websocket upgrade
INFO:werkzeug:127.0.0.1 - - [08/Aug/2022 01:16:37] "GET /socket.io/?transport=websocket&EIO=4&t=1659946597.8447661 HTTP/1.0" 400 -

What i've tried so far Changing the https:// in the sock.connect() call to wss:// Modifying the order of the proxy directives in NGINX Adding HTTP version directive in NGINX