gpt4 book ai didi

android - 在 Android 中与 Xmpp 服务器保持实时连接的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:05 24 4
gpt4 key购买 nike

我正在开发聊天应用程序并使用 ejabberd saas 版本作为它的 xmpp 服务器。我正在使用 smack 库 ver-4.2.3。为了保持连接,我正在使用 ping 管理器。这是我正在使用的代码:

ReconnectionManager.getInstanceFor(AppController.mXmpptcpConnection).enableAutomaticReconnection();
ServerPingWithAlarmManager.onCreate(context);
ServerPingWithAlarmManager.getInstanceFor(AppController.mXmpptcpConnection).setEnabled(true);
ReconnectionManager.setEnabledPerDefault(true);

//int i = 1;
// PingManager.setDefaultPingInterval(i);
PingManager.getInstanceFor(AppController.mXmpptcpConnection).setPingInterval(300);

我也在使用粘性服务进行连接,但是当我将我的应用程序保持打开状态(理想状态)15-20 分钟时,连接就会丢失,所以我使用 ping 管理器来解决这个问题。

有没有其他更好的方法,或者 ping 管理器是唯一的选择?

最佳答案

为了不断地 ping 聊天服务器,你最好在 smack 库中使用 ConnectionListener()。你需要使用这样的东西:

XMPPTCPConnection connection;
// initialize your connection

// handle the connection
connection.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {

}

@Override
public void authenticated(XMPPConnection connection, boolean resumed) {

}

@Override
public void connectionClosed() {
// when the connection is closed, try to reconnect to the server.
}

@Override
public void connectionClosedOnError(Exception e) {
// when the connection is closed, try to reconnect to the server.
}

@Override
public void reconnectionSuccessful() {

}

@Override
public void reconnectingIn(int seconds) {

}

@Override
public void reconnectionFailed(Exception e) {
// do something here, did you want to reconnect or send the error message?
}
});

关于android - 在 Android 中与 Xmpp 服务器保持实时连接的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934923/

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