Java XMPP aSmack-如何在自己的用户状态(在线/离线)上侦听以重新连接
我是android中xmpp/asmack的新手,我正在寻找一种方法来监听我自己在服务器上的用户状态和状态变化 我的目标是恢复连接(如果丢失) 我使用的是按花名册显示的状态,这有助于我获得好友的状态,但实际上不是当前用户本身 任何帮助都将不胜感激:)Java XMPP aSmack-如何在自己的用户状态(在线/离线)上侦听以重新连接,java,android,xmpp,asmack,Java,Android,Xmpp,Asmack,我是android中xmpp/asmack的新手,我正在寻找一种方法来监听我自己在服务器上的用户状态和状态变化 我的目标是恢复连接(如果丢失) 我使用的是按花名册显示的状态,这有助于我获得好友的状态,但实际上不是当前用户本身 任何帮助都将不胜感激:) 致以最诚挚的问候,您必须启用重新连接管理器 例如: XmppManager.config = XMPPTCPConnectionConfiguration.builder() .setServiceName(serv
致以最诚挚的问候,您必须启用重新连接管理器 例如:
XmppManager.config = XMPPTCPConnectionConfiguration.builder()
.setServiceName(serverName)
.setHost(server)
.setPort(port)
.build();
connection = new XMPPTCPConnection(config);
ConnectionListener connectionListener = new ConnectionListener(){...}; //
connection.addConnectionListener( connectionListener );
int RECONNECTION_ATTEMPT_SECONDS = 60;
ReconnectionManager.getInstanceFor(connection).enableAutomaticReconnection();
ReconnectionManager.getInstanceFor(connection).setFixedDelay( RECONNECTION_ATTEMPT_SECONDS );
重新连接侦听器如下所示:
public class ReconnectionListener implements ConnectionListener
{
@Override
public void reconnectionSuccessful()
{
System.out.println( "Connection to chat server restored - You are again online" );
//additional foo when connection restored
}
@Override
public void reconnectionFailed( Exception e )
{
System.out.println("Impossible to reconnect, Chat Server seems to be still unavailable" );
}
@Override
public void reconnectingIn( int seconds )
{
System.out.println( "reconnectingIn fired "+seconds);
}
@Override
public void connectionClosedOnError( Exception e )
{
System.out.println("Connection closed, Chat Server become unavailable" );
//additional foo when connection lost (message to user ?)
}
@Override
public void connectionClosed()
{
// "XMPP connection was closed.");
System.out.println( "Connection closed, Chat Server become unavailable");
}
@Override
public void connected( XMPPConnection connection )
{
System.out.println("connected fired - reconnection management enabled");
}
@Override
public void authenticated( XMPPConnection connection, boolean resumed )
{
System.out.println("authenticated fired");
}
}
如果这有帮助,请不要忘记接受答案:)要实现自动重新连接,您应该使用重新连接管理器,并实现接口ConnectionListener以获取通知
有关详细信息,请参见
https://ramzandroidarchive.wordpress.com/2016/03/14/handling-connection-break-issue-in-smack-4-1/
还有另一种使用计时器重新连接的方法:
public class TaxiConnectionListener implements ConnectionListener {
private Timer tExit;
private String username;
private String password;
private int logintime = 2000;
@Override
public void connectionClosed() {
Log.i("TaxiConnectionListener", "disconnect");
XmppConnection.getInstance().closeConnection();
tExit = new Timer();
tExit.schedule(new timetask(), logintime);
}
@Override
public void connectionClosedOnError(Exception e) {
Log.i("TaxiConnectionListener", "failed to disconnect");
boolean error = e.getMessage().equals("stream:error (conflict)");
if (!error) {
XmppConnection.getInstance().closeConnection();
tExit = new Timer();
tExit.schedule(new timetask(), logintime);
}
}
class timetask extends TimerTask {
@Override
public void run() {
username = Utils.getInstance().getSharedPreferences("taxicall",
"account", MainActivity.context);
password = Utils.getInstance().getSharedPreferences("taxicall",
"password", MainActivity.context);
if (username != null && password != null) {
Log.i("TaxiConnectionListener", "logining");
if (XmppConnection.getInstance().login(username, password)) {
Log.i("TaxiConnectionListener", "logined");
} else {
Log.i("TaxiConnectionListener", "reconnect");
tExit.schedule(new timetask(), logintime);
}
}
}
}
@Override
public void reconnectingIn(int arg0) {
}
@Override
public void reconnectionFailed(Exception arg0) {
}
@Override
public void reconnectionSuccessful() {
}
}
您需要在登录方法中添加连接侦听器:
TaxiConnectionListener connectionListener = new TaxiConnectionListener();
getConnection().addConnectionListener(connectionListener);
在断开连接方法中删除侦听器:
connection.removeConnectionListener(connectionListener);
你到底想做什么?我想保持XMPP连接稳定,所以如果我的用户断开连接(脱机),我必须自动重新连接。。。。。