socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
60.54k stars 10.09k forks source link

On error code 400 for wss request socket.io doesnt fall-back into long polling #2489

Closed magestican closed 7 years ago

magestican commented 8 years ago

I have an issue were the proxy of on of my clients is negating wss requests, but the fall-back mechanism of socket.io doesnt take place if this is the case, specifically on a 400 bad request returned code from the server, can you look into it?

dsalcedo commented 8 years ago

In the client side

var socket = io(http, {transports: ['polling','websocket']});

may be the problem is this: in their proxy is allowed *.example.com:80 and your socket is running in example.com:5000 , why not you create inverse proxy (using nginx) for using something like realtime.example.com https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/

darrachequesne commented 7 years ago

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

yingshaoxo commented 4 years ago

I've been spending a whole night to solve this problem when I start to use https or wss or ssl. It always says connection stopped before establish with 400 error code.

Just a minutes ago, I found a solution for that:

0. Cloudflare

At the SSL/TLS tab:

If you are not sure what you are doing, just go to DNS tab, set DNS only

1. Make sure you have a right proxy configuration.

server {
    listen 80;
    server_name ai-tools-online.xyz;
    return 301 https://ai-tools-online.xyz$request_uri;
}

server {
    listen 443 ssl http2;

    ssl_certificate       /data/v2ray.crt;
    ssl_certificate_key   /data/v2ray.key;
    ssl_protocols         TLSv1.2 TLSv1.3;
    #ssl_ciphers           3DES:RSA+3DES:!MD5;
    server_name ai-tools-online.xyz;

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

    location /socket.io {
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://127.0.0.1:5000/socket.io;
    }
}

ai-tools-online.xyz is your domain, http://127.0.0.1:5000 is your socket server.

2. Make sure your server Cross-Origin Controls is set to '*' to allow Cross-Origin Access

For flask-socketio, is to use flask_socketio.SocketIO(app, cors_allowed_origins = '*')

3. You must restart the nginx to let the new config work

systemctl restart nginx

4. For more details about how to set caddy, see the following links:

https://github.com/yingshaoxo/Web-Math-Chat#reverse-proxy-configuration-for-https https://caddy.community/t/using-caddy-0-9-1-with-socket-io-and-flask-socket-io/508/6 https://www.nginx.com/blog/nginx-nodejs-websockets-socketio/