javaandroidxmppasmack

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.

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,


Solution

  • You have to enable a 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:

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