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

时间:2017-01-23 12:26:08

标签: java android xmpp asmack

我是android的新手xmpp / asmack,我正在寻找一种方法来监听我自己的用户状态和服务器上的状态变化。

我的目标是丢失时恢复连接。

我正在使用名册,这有助于我吸引朋友,但实际上不是当前的用户。

任何帮助将不胜感激:)

致以最诚挚的问候,

3 个答案:

答案 0 :(得分:0)

您必须启用ReconectionManager。

示例:

    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如下所示:

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

    }

如果有帮助,请不要忘记接受答案:)

答案 1 :(得分:0)

要实现自动重新连接,应使用ReconnectionManager,并实现ConnectionListener接口以获取通知。

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

中引用

答案 2 :(得分:0)

存在使用计时器重新连接的另一种方法:

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