Search code examples
androidcallbackrx-javaasp.net-core-signalr

Android - connectionState callback missing for ASP.NET Core SignalR for Android?


I have the following connection code -

 private val authorization = App.context?.getSharedPreferences(authorizationPrefs, Context.MODE_PRIVATE)?.getString(authorizationToken, defaultStringValue)
    var hubConnection: HubConnection? = null

    fun startConnection() {
        if (hubConnection != null) return
        hubConnection = HubConnectionBuilder.create(Constants.twoverteBaseUrl.plus(SignalREndpoints.accessSecureChatQuery))
            .withHeader("Authorization", authorization).build().apply { start() }
    }

I am missing the open to know exactly when connection is connected as it takes a few milisec and if I put my code after that start() I will get connectionState as DISCONNECTED because it did not have the time to connect yet.

I am sure there is a callback for this one.

How do I put the connectionState callback here?


Solution

  • hubConnection.start() returns an RxJava Completable, so if you want to do things once the connection has started, you need to subscribe to the Completable and run some code when it completes.

    Example:

    hubConnection.start().subscribe(() -> {
    // connected
    },
    error -> {
    // error
    });