gpt4 book ai didi

android - wifi关闭并再次打开时Websocket重新连接问题

转载 作者:行者123 更新时间:2023-12-04 22:41:28 24 4
gpt4 key购买 nike

我遇到了 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)
}
}

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

最佳答案

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

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

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

关于android - wifi关闭并再次打开时Websocket重新连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59681449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com