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

Change/update auth or query or extraheaders containing token after connection #752

Closed enlight3d closed 10 months ago

enlight3d commented 10 months ago

Hello, I already checked the issues here concerning my question, but I can't get this working. I tried to implement what is mentioned here : https://socketio.github.io/socket.io-client-java/initialization.html#auth concerning updating the map but I always get errors when I try to do that.

The provided code is for the java, I tried to adapt it for kotlin this way:

         mSocket.io().on(Manager.EVENT_RECONNECT_ATTEMPT) {
            Log.i("MessagingClient", "EVENT_RECONNECT_ATTEMPT")
            val token = runBlocking {
                tokenManager.getAccessToken()
            }
            mSocketOptions?.extraHeaders?.set("authorization", listOf("Bearer ${token}"))
        }

Concerning the errors that I get, it's the same as mentioned in this issue https://github.com/socketio/socket.io-client-java/issues/630

I'm using io.socket:socket.io-client:2.0.0

enlight3d commented 10 months ago

Well, I'm closing this issue, for future people searching for the same thing, you can't use .set but you must do it this way :

mSocketOptions?.extraHeaders = mapOf("Authorization" to listOf("Bearer $token"))
mSocket.disconnect().connect()