Skip to content

Java socket io client connection disconnects #723

Open
@Areej-Fatima

Description

@Areej-Fatima

I have implemented socket.io client side in java spring boot, using thread pool executor. Now my socket connections are build successfully, but after 50s to 1 minute, they start getting disconnected. How to sustain socket connections? My code is as below

    CountDownLatch lock = new CountDownLatch(200);
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(200);
    ScheduledFuture<?> future;
        for(int i=1;i<=200;i++) {
       
        future= executor.scheduleAtFixedRate(() -> {

            Map<String, String> map = new HashMap<String, String>();
            map.put("token", "abc");

            IO.Options options = IO.Options.builder()
                    .setPath("/socket.io")
                    .setTransports(new String[]{Polling.NAME,WebSocket.NAME})
                    .setAuth(map)
                    .build();

            Socket socket = IO.socket(URI.create("wss://localhost:3000"), options);

            socket.on("connect_error", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println(args[0]);
                }
            });

            socket.on("connect", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Socket connection "+i+" successfully created");
                }
            });
            socket.on("disconnect", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Socket connection "+i+" disconnected");
                }
            });

            socket.connect();
            lock.countDown();
        }, 1000, delay, TimeUnit.MILLISECONDS);

        lock.await(delay, TimeUnit.MILLISECONDS);
        future.cancel(true);

    }//end of for loop
    executor.shutdown();

My java socket.io-client 2.1.0 is compatible with socket.io server v4.x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions