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.31k stars 969 forks source link

How to listen to Manager events? (Socket events are working) #754

Closed Canato closed 10 months ago

Canato commented 10 months ago

First of all, thanks, @darrachequesne, for maintaining this lib. I know how hard it can be. If you open the sponsorship option, I would be happy to contribute.

Context

I'm trying to make my code react when there is a reconnection attempt. I considered listening to the manager events like I do with the socket ones. Didn't work.

I saw we had some active changes https://github.com/socketio/socket.io-client-java/issues/643, but I can't figure out how to use it.

Code

Something like this:

socket = IO.socket(url, options).connect().apply {
    on(Socket.EVENT_CONNECT) {
        Log.i("Canatoxxx", "OPEN: ${Socket.EVENT_CONNECT}")
        trySend(CustomEvent.Connect)
    }
    on(Manager.EVENT_RECONNECT_FAILED) {
        Log.i("Canatoxxx", "OPEN: ${Manager.EVENT_RECONNECT_FAILED}")
        trySend(CustomEvent.ReconnectionFailed)
    }
    (...)
    onAnyIncoming {
        Log.i("Canatoxxx", "Incoming: $it")
    }
    onAnyOutgoing {
        Log.i("Canatoxxx", "Outgoing: $it")
    }
}

Logs

But my logs don't show the Manager events when I turn off and on the wifi.

2023-10-24 12:56:02.187  7666-7728  Canatoxxx  app.development  I  OPEN: connect
2023-10-24 12:56:02.222  7666-7728  Canatoxxx  app.development  I  Outgoing: [Ljava.lang.Object;@f8987a
2023-10-24 12:56:02.223  7666-7728  Canatoxxx  app.development  I  Outgoing: [Ljava.lang.Object;@aa482b
2023-10-24 12:56:02.256  7666-7838  Canatoxxx  app.development  I  Incoming: [Ljava.lang.Object;@5da4ff
2023-10-24 12:56:14.216  7666-7907  Canatoxxx  app.development  I  OPEN: disconnect
2023-10-24 12:56:15.256  7666-7907  Canatoxxx  app.development  I  OPEN: connect_error
2023-10-24 12:56:18.218  7666-7907  Canatoxxx  app.development  I  OPEN: connect_error
2023-10-24 12:56:24.266  7666-7899  Canatoxxx  app.development  I  OPEN: connect

Resume

This could be for one of two reasons:

Questions

  1. Is wifi On/Off enough to test the reconnection feature?
  2. How can I listen to events (or manager events in general)?

Thank you in advance

Edit

Options

val options = IO.Options().apply {
    forceNew = true
    secure = true
    transports = arrayOf(WebSocket.NAME)
    reconnectionAttempts = 5
    reconnection = true
    reconnectionDelay = 1000
    reconnectionDelayMax = 5000
    optionsBuilder()
}
darrachequesne commented 10 months ago

Hi! The reconnection events are emitted by the Manager, which can be accessed with socket.io():

socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, () -> {
  // ...
});
Canato commented 10 months ago

Ahhh I see!

I was doing

socket = IO.socket(url, options).connect().apply {
    on(Socket.EVENT_CONNECT) {
        Log.i("Canatoxxx", "OPEN: ${Socket.EVENT_CONNECT}")
        trySend(CustomEvent.Connect)
    }
    on(Manager.EVENT_RECONNECT_FAILED) {
        Log.i("Canatoxxx", "OPEN: ${Manager.EVENT_RECONNECT_FAILED}")
        trySend(CustomEvent.ReconnectionFailed)
    }
    (...)
}

And should be

socket = IO.socket(url, options).connect().apply {
    on(Socket.EVENT_CONNECT) {
        Log.i("Canatoxxx", "OPEN: ${Socket.EVENT_CONNECT}")
        trySend(CustomEvent.Connect)
    }
    io().apply {
        on(Manager.EVENT_RECONNECT_FAILED) {
            Log.i("Canatoxxx", "OPEN: ${Manager.EVENT_RECONNECT_FAILED}")
            trySend(CustomEvent.ReconnectionFailed)
        }
    }
    (...)
}

Thanks!

I will do some tests and maybe open a PR with documentation suggestions to make it more clear =D