socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.32k stars 972 forks source link

socket.io-client-java repeatedly disconnecting and connecting to my nodeJs backend server with nginx #715

Open zhikecore opened 2 years ago

zhikecore commented 2 years ago

Hi, I've an issue where the sockets is repeatedly disconnecting and connecting to my nodeJs backend server. And I have configured Nginx as Reverse Proxy for WebSocket for 2 nodeJs webservers.

Such as,

server {
    listen 5000;
    server_name localhost;

    access_log  /data/logs/nginx/webserver/access.log  main;

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

      proxy_pass http://webserver-nodes;

      # enable WebSockets
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
  }

  upstream webserver-nodes {
    # enable sticky session based on IP
    # ip_hash;

    server 172.xx.xx.xx:5001;
    server 172.xx.xx.xx:5002;
  }

The webserver socket.op version is "socket.io": "^2.4.1".

According this table:

Client version Socket.IO server
0.9.x 1.x
1.x 2.x
2.x 3.x / 4.x

I selected socket.io-client-v1.0.0/1.0.1. I have one client to connect the webserver.

My code is here:

try
        {
            String url ="http://xx.xx.xx.xx:5000";
            IO.Options options = new IO.Options();
            options.forceNew = true;
            options.reconnectionDelay = 1000;
            options.timeout = 5000;

            socket = IO.socket(URI.create(url), options);
            //socket.connect();
        }catch (Exception ex)
        {
            log.error("connect error:"+ex);
        }

when I set url ="http://xx.xx.xx.xx:5000".

I find the socket java client disconnects and connects repeatedly during the 2 webservers.

But,when I connect the define webserver,such as when I set url ="http://xx.xx.xx.xx:5001". It works.

Any way, if we use JavaScript socketio-client in other project,when set url ="http://xx.xx.xx.xx:5000",it works.

So, What should I do?

GingerAdonis commented 2 years ago

Make sure to uncomment ip_hash; in your nginx configuration if you have multiple backends.