2017-01-23 46 views
0

我是新來的xmpp/asmack在android中,我正在尋找一種方法來偵聽我自己的用戶狀態和存在變化服務器。XMPP aSmack - 我怎麼能聽我自己的用戶狀態(在線/離線)重新連接

我的目標是恢復連接,如果丟失。

我使用花名冊來幫助我獲得朋友的存在,但實際上並不是當前用戶本身。

任何幫助,將不勝感激:)

最好的問候,

+0

你想要做什麼其實? –

+0

我想保持XMPP連接穩定,所以我必須自動重新連接,如果我的用戶斷開連接(離線)..... – Manar18

回答

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

    } 

如果幫助,請不要忘記接受的答案:)

+0

這正是一個正在尋找....謝謝你@MrPk :) – Manar18

相關問題