【问题标题】:Websocket reconnecting issue when wifi turned off and turned on againwifi关闭并再次打开时Websocket重新连接问题
【发布时间】:2020-04-28 02:09:58
【问题描述】:

我遇到了 websocket 重新连接的问题。第一次启动应用程序时,创建 websocket 连接没有问题。然后我关闭了wifi,几秒钟后我再次打开了wifi,我在onFailure回调中得到了以下异常。

WS onFailure javax.net.ssl.SSLException: Read error: ssl=0xbf4618c0: I/O error during system call, Software caused connection abort
    E/Mqtt: WS onFailure java.io.InterruptedIOException: executor rejected
E/Mqtt: WS onFailure java.io.InterruptedIOException: executor rejected
E/Mqtt: WS onFailure java.io.InterruptedIOException: executor rejected
E/Mqtt: WS onFailure java.io.InterruptedIOException: executor rejected
E/Mqtt: WS onFailure java.io.InterruptedIOException: executor rejected

这是我的连接方法:

public fun connect()
{
    if(mConnecting)
        return

    mConnecting = true

    val request = Request.Builder().url(mSocketUrl).build()

    mWebSocket = mOkHttpClient.newWebSocket(request, this)
    mOkHttpClient.dispatcher().executorService().shutdown()

}

这里是 websocket onFailure 回调

override fun onFailure(webSocket: WebSocket?, t: Throwable, response: Response?)
{
    Log.e(TAG, "WS onFailure $t")

    mConnecting = false
    reconnect()
}

这是我的重新连接方法

    public fun reconnectMqtt()
    {
        if(mStopping)
            return

        if(mMqttConnection == null)
        {
            startMqtt()
            return
        }

        if(!mMqttConnection!!.mConnecting)
        {
//            mMqttConnection = null
//            startMqtt()

            Handler(Looper.getMainLooper()).postDelayed({
                mMqttConnection!!.connect()
            }, 1000)
        }
    }

我搜索了很多,但没有得到任何帮助。任何帮助将不胜感激。提前致谢!!

【问题讨论】:

  • 出于任何原因,您要重新连接自己,而不是让 MQTT 客户端库(paho?)为您完成。
  • mqtt 是如何自动做到这一点的?
  • 重新连接是 Paho MQTT 库中的内置功能
  • 所以你建议我不要在 onFailure 中调用 reconnect 方法
  • 我试过了,但它没有重新连接,我已经实现了 MqttCallbackExtended 监听器来重新连接状态,但是这个监听器没有调用

标签: android ssl websocket mqtt okhttp


【解决方案1】:

问题在于连接方法中的以下行。

mOkHttpClient.dispatcher().executorService().shutdown()

关闭方法不允许我重新连接 websocket。这是释放资源所必需的。我将此方法调用转移到服务 onDestroy。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多