简体   繁体   English

XMPP aSmack-如何监听自己的用户状态(在线/离线)以重新连接

[英]XMPP aSmack - How can I listen on my own user state (Online/Offline) to Reconnect

I am new to xmpp/asmack in android, i'm looking for a method to listen on my own user state and presence changes on the server. 我是android中的xmpp / asmack的新手,我正在寻找一种侦听我自己的用户状态和服务器上状态更改的方法。

My target it's restore connection if lost. 我的目标是丢失时恢复连接。

I'm using presence by roster, which helps me getting the friends presence but actually not the current user itself. 我使用的是按名单显示状态,这可以帮助我获得朋友的状态,但实际上不是当前用户本身。

Any help would be appreciated :) 任何帮助,将不胜感激 :)

Best regards, 最好的祝福,

You have to enable a ReconectionManager. 您必须启用ReconectionManager。

Example: 例:

    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 );

ReconnectionListener looks like this: ReconnectionListener看起来像这样:

   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");      
        }

    }

If that helped, please don't forget to accept the answer :) 如果有帮助,请不要忘记接受答案:)

To realize the automatic reconnection, you should utilize the ReconnectionManager, and implement the interface ConnectionListener to get the notification. 要实现自动重新连接,您应该利用ReconnectionManager,并实现ConnectionListener接口来获取通知。

Details can be refered in https://ramzandroidarchive.wordpress.com/2016/03/14/handling-connection-break-issue-in-smack-4-1/ 可以在https://ramzandroidarchive.wordpress.com/2016/03/14/handling-connection-break-issue-in-smack-4-1/引用详细信息

There exist another way to reconnect by using a timer: 存在使用计时器重新连接的另一种方法:

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() {
    }
}

You need add a connection listener in your logining method: 您需要在登录方法中添加连接侦听器:

TaxiConnectionListener connectionListener = new TaxiConnectionListener();
getConnection().addConnectionListener(connectionListener);

Remove listener in disconnection method: 在断开连接方法中删除侦听器:

 connection.removeConnectionListener(connectionListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM