4

我在 Android 上有一个项目,需要使用 asmack 库来启用实时聊天。

关键功能是重新连接机制,这意味着我的应用程序可以在互联网连接丢失时自动重新连接。

这是我的代码片段:

ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);
connConfig.setSASLAuthenticationEnabled(true);
connConfig.setReconnectionAllowed(true);
XMPPConnection connection = new XMPPConnection(connConfig);

但是每当我关闭然后打开 Wifi 时,我都会收到此错误:

10-01 21:43:26.942: W/System.err(13695): javax.net.ssl.SSLException: Write error: ssl=0x5a4ad348: I/O error during system call, Broken pipe
10-01 21:43:27.002: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
10-01 21:43:27.012: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:693)
10-01 21:43:27.012: W/System.err(13695):    at java.io.OutputStreamWriter.flushBytes(OutputStreamWriter.java:167)
10-01 21:43:27.012: W/System.err(13695):    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:158)
10-01 21:43:27.012: W/System.err(13695):    at java.io.BufferedWriter.flush(BufferedWriter.java:124)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter.writePackets(PacketWriter.java:210)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter.access$000(PacketWriter.java:42)
10-01 21:43:27.012: W/System.err(13695):    at org.jivesoftware.smack.PacketWriter$1.run(PacketWriter.java:78)
10-01 21:43:27.092: W/System.err(13695): javax.net.ssl.SSLException: Read error: ssl=0x5a4ad348: I/O error during system call, Connection timed out
10-01 21:43:27.092: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
10-01 21:43:27.092: W/System.err(13695):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:651)
10-01 21:43:27.092: W/System.err(13695):    at java.io.InputStreamReader.read(InputStreamReader.java:244)
10-01 21:43:27.092: W/System.err(13695):    at java.io.BufferedReader.read(BufferedReader.java:310)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1496)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:979)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.next(KXmlParser.java:346)
10-01 21:43:27.092: W/System.err(13695):    at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:326)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
10-01 21:43:27.092: W/System.err(13695):    at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)

谁能解决这个错误,请帮助我!

4

4 回答 4

8

使用最新的 smack api - 我使用的是 4.1.0

然后设置重新连接,如下所示:

ReconnectionManager manager = ReconnectionManager.getInstanceFor(connection);
manager.enableAutomaticReconnection();

它将按照此处所述自动重新连接

于 2015-05-26T06:51:52.580 回答
0

Asmack 正确支持重新连接,唯一的问题是您正在使用 SASL 连接,所以这会出现问题,因为您在关闭 Wi-Fi 时再次尝试相同的连接。尝试使用 set SASL false

connConfig.setSASLAuthenticationEnabled(false);

需要其他方式手动断开

connection.disconnect();
于 2014-06-25T17:52:26.323 回答
0

在开始设置所有内容时,请致电

ReconnectionManager.setEnabledPerDefault(true);

然后从那时起创建的连接将自动重新连接。

于 2016-08-21T11:47:21.467 回答
-1

Don't use reconnection mechanism which is implemented in ASMACK.

Use:

    connConfig.setReconnectionAllowed(false);

Instead, you should create BroadcastReceiver which listens to ConnectivityManager.CONNECTIVITY_ACTION. When connection to WIFI or other network is occurred, then you will receive the callback onReceive in your BroadcastReceiver.

于 2013-11-19T15:40:19.537 回答