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.34k stars 975 forks source link

Constant "xhr poll error" when trying to establish a connection #645

Closed ParadoxPixel closed 3 years ago

ParadoxPixel commented 3 years ago

Describe the bug The problem I'm having is that the client is unable to connect to the socket.io server. I get a "xhr poll error" every time I try to connect but it used to work fine and then out of the blue it started having issues.

To Reproduce

Socket.IO server: https://github.com/Mindgamesnl/socketio

Server

package main

import (
    "log"
    "net/http"
    "time"

    "github.com/Mindgamesnl/socketio"
)

func main() {
    server, _ := socketio.NewServer(time.Second*25, time.Second*5, socketio.DefaultParser)
    server.Namespace("/").
        OnConnect(func(so socketio.Socket) {
            log.Println("connected:", so.RemoteAddr(), so.Sid(), so.Namespace())
        }).
        OnDisconnect(func(so socketio.Socket) {
            log.Printf("%v %v %q disconnected", so.Sid(), so.RemoteAddr(), so.Namespace())
        }).
        OnError(func(so socketio.Socket, err ...interface{}) {
            log.Println("socket", so.Sid(), so.RemoteAddr(), so.Namespace(), "error:", err)
        }).
        OnEvent("message", func(so socketio.Socket, data string) {
            log.Println(data)
        })

    http.ListenAndServe(":4040", server)
}

Socket.IO java client version: 2.0.0

Client

public class MyApplication {
    public static void main(String[] args) throws URISyntaxException {
        IO.Options opts = new IO.Options();
        opts.callFactory = okHttpClient;
        opts.reconnection = false;
        opts.webSocketFactory = okHttpClient;

        // authentication headers
        opts.query = String.format(
                "type=server&private=%s&public=%s",
                keySet.getPrivateKey(),
                keySet.getPublicKey()
        );

        //Setup socket.io connection
        Socket socket;
        try {
            socket = IO.socket("http://localhost:4040", opts);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

       socket.on(Socket.EVENT_CONNECT_ERROR, args -> {
            System.out.print(args[0].toString());
        });

        socket.open();
    }
}

Expected behavior It should connect to the websocket and not directly drop the connection

Platform:

Additional context The Socket.IO server works fine for clients connecting from the browser hense why I deduced that it might be an issue with the java client.

I used the java client version 1.0.0 before this but it started instantly dropping the connection after it was just established out of the blue. A little later it stopped connecting altogether and only gave me a "transport error". An important note on this is that it worked fine before but suddenly gave me these issues.

Edit: I also tried Netty-Socket.IO and used Socket.IO java client version: 1.0.0 due to compatibility and this gave me the same error

ParadoxPixel commented 3 years ago

Nvm, after some more research and debugging turned out I forgot about some security measures I had in place.

KillerQueen59 commented 3 years ago

can i see ur code after solved that?

ParadoxPixel commented 3 years ago

The error was caused by me closing the connection during the connect event

Luqman-Khokhar commented 2 months ago

Im getting this issue on mobile side (react native) suddenly happened , in website (react JS) not doing harm. Due to mobile issue it also crashed the website too. it can be due to following reasons. FRONT-END

  1. multiple instance of io client url
  2. alot of request at a time .

    BACK-END

  3. CORS error
  4. blocked from server

    COMMON

  5. Network issue
  6. VPN / FIREWALL