Junaid Bashir
Junaid Bashir

Reputation: 172

Websocket reconnecting issue when wifi turned off and turned on again

i am having an issue with websocket reconnection. First time when app launch there is no issue with creating websocket connection. then i turned off wifi and after few seconds i turned on wifi again i get the below exception in my onFailure callback.

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

Here is my connect method:

public fun connect()
{
    if(mConnecting)
        return

    mConnecting = true

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

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

}

Here is websocket onFailure callback

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

    mConnecting = false
    reconnect()
}

and here is my reconnect method

    public fun reconnectMqtt()
    {
        if(mStopping)
            return

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

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

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

i have searched a lot but didn't get anything helping. any help will be appreciated. Thanks in advance!!

Upvotes: 1

Views: 2386

Answers (1)

Junaid Bashir
Junaid Bashir

Reputation: 172

Issue was with this below line in connect method.

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

the shutdown method was not allowing me to reconnect websocket. this is necessary for releasing resources. i shift this method call to service onDestroy.

Upvotes: 2

Related Questions