【问题标题】:Android - connectionState callback missing for ASP.NET Core SignalR for Android?Android - 适用于 Android 的 ASP.NET Core SignalR 缺少 connectionState 回调?
【发布时间】:2020-04-12 12:39:17
【问题描述】:

我有以下连接代码 -

 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() }
    }

我错过了确切知道连接何时连接的开放时间,因为它需要几毫秒,如果我将我的代码放在之后 start() 我将得到 connectionState 为 DISCONNECTED 因为它还没有时间连接.

我确信有一个回调。

如何将 connectionState 回调放在这里?

【问题讨论】:

    标签: android callback rx-java asp.net-core-signalr


    【解决方案1】:

    hubConnection.start() 返回一个RxJava Completable,所以如果你想在连接开始后做一些事情,你需要订阅 Completable 并在它完成时运行一些代码。

    例子:

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

    【讨论】:

    • 刚刚尝试过,但我无法得到它。你能按照我的例子输入吗?
    • 我不知道 Kotlin,但应该是 java fun startConnection() { if (hubConnection != null) return hubConnection = HubConnectionBuilder.create(Constants.twoverteBaseUrl.plus(SignalREndpoints.accessSecureChatQuery)) .withHeader("Authorization", authorization).build().apply { start().subscribe(() -> { // connected }, error -> { // error });} }
    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多