在 android 中聊天时 Quickblox Chat 连接中断

Quickblox Chat connection drop when chatting in android

我正在为 android 开发一款涉及 Quickblox 的应用程序。我不断收到消息 'Chat connection has been dropped'。阅读 logcat 后,我得到的是:

12-08 11:15:31.214 13894-14722/com.quickblox.sample W/AbstractXMPPConnection: Connection closed with error java.net.SocketTimeoutException: Read timed out at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method) at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:705) at java.io.InputStreamReader.read(InputStreamReader.java:233) at java.io.BufferedReader.read(BufferedReader.java:325) at org.jivesoftware.smack.util.ObservableReader.read(SourceFile:41) at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515) at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992) at org.kxml2.io.KXmlParser.next(KXmlParser.java:349) at org.kxml2.io.KXmlParser.next(KXmlParser.java:313) at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(SourceFile:1240) at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access0(SourceFile:1014) at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.run(SourceFile:1029) at java.lang.Thread.run(Thread.java:818) 12-08 11:15:31.216 13894-14722/com.quickblox.sample D/SMACK: XMPPConnection closed due to an exception (0) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: java.net.SocketTimeoutException: Read timed out 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:705) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at java.io.InputStreamReader.read(InputStreamReader.java:233) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at java.io.BufferedReader.read(BufferedReader.java:325) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.jivesoftware.smack.util.ObservableReader.read(SourceFile:41) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1515) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:992) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.kxml2.io.KXmlParser.next(KXmlParser.java:349) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.kxml2.io.KXmlParser.next(KXmlParser.java:313) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(SourceFile:1240) 12-08 11:15:31.216 13894-14722/com.quickblox.sample W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access0(SourceFile:1014) 12-08 11:15:31.217 13894-14722/com.quickblox.sample W/System.err: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.run(SourceFile:1029) 12-08 11:15:31.217 13894-13894/com.quickblox.sample I/VerboseQbChatConnectionListener: connectionClosedOnError(): Read timed out 12-08 11:15:31.217 13894-14722/com.quickblox.sample W/System.err: at java.lang.Thread.run(Thread.java:818)

我找不到相同的解决方案。

套接字是您用来通过网络进行通信的工具(例如,连接到 Internet 上的服务器)。该错误信息表示程序试图通过网络从远程计算机接收数据,但是一段时间没有接收到数据,因此程序停止等待接收数据。

这是可以通过增加套接字超时来解决此问题的代码片段,因为 Quickblox 中的默认 SocketTimeOut 是 30 秒。

QBChatService.ConfigurationBuilder chatServiceConfigurationBuilder = new QBChatService.ConfigurationBuilder();
chatServiceConfigurationBuilder.setSocketTimeout(180); //Sets chat socket's read timeout in seconds
chatServiceConfigurationBuilder.setKeepAlive(true); //Sets connection socket's keepAlive option.
QBChatService.setConfigurationBuilder(chatServiceConfigurationBuilder);