我正在制作一款Android聊天应用。我使用api level 23,Xmpp Smack lib版本4.1.8,在服务器上我使用ejabberd,版本16.06。 我使用此代码来构建XMPP连接。 (TIMEOUT = 5000)
private void buildConnection() {
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder()
.setServiceName(mServiceName)
.setHost(mServiceName)
.setConnectTimeout(TIMEOUT)
.setResource("Smack")
.setUsernameAndPassword(mUsername, mPassword)
.setSendPresence(true)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
mConnection = new XMPPTCPConnection(builder.build());
mConnection.setPacketReplyTimeout(TIMEOUT);
chatManager = ChatManager.getInstanceFor(mConnection);
chatManager.addChatListener(this);
PingManager pingManager = PingManager.getInstanceFor(mConnection);
pingManager.setPingInterval(5);
pingManager.registerPingFailedListener(this);
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
reconnectionManager.enableAutomaticReconnection();
mConnection.addConnectionListener(this);
if (mConnection != null) {
DeliveryReceiptManager.getInstanceFor(mConnection).addReceiptReceivedListener(new ReceiptReceivedListener() {
@Override
public void onReceiptReceived(String fromJid, String toJid, String receiptId, Stanza receipt) {
updateMessageStatusReceived(receipt);
Log.i(TAG, "Confirmation stanza Got: " + receipt);
}
});
}
}
(updateMessageStatusReceived(接收)方法仅用于刷新视图)。
之后,我连接并验证。
Everthing是正常的,但是当连接断开时,我需要在恢复连接时重新发送失败的消息。我没有在Smack中找到任何解决方案或事件来检测消息是否失败。有时确认服务器节的延迟很高,我的应用程序假设消息失败但没有,并且另一方收到多次相同的消息。
我构建一个Message对象并使用以下代码发送:
try {
Chat chat = chatManager.createChat(chatMessage.getJid());
chat.sendMessage(message);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
当连接丢失并且我发送消息时,不会抛出任何异常。
我的问题是:我如何知道该消息真的失败了,再试一次? 谢谢,对不起英语。