Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to keepAlive Connection With Xmpp Server in Android

I am working on chat application and using ejabberd saas edition as xmpp server for it. I am using smack library ver-4.2.3. To keep connection alive I am using ping manager. Here is the code I am using:

ReconnectionManager.getInstanceFor(AppController.mXmpptcpConnection).enableAutomaticReconnection();
ServerPingWithAlarmManager.onCreate(context);
ServerPingWithAlarmManager.getInstanceFor(AppController.mXmpptcpConnection).setEnabled(true);
ReconnectionManager.setEnabledPerDefault(true);

//int i = 1;
// PingManager.setDefaultPingInterval(i);
PingManager.getInstanceFor(AppController.mXmpptcpConnection).setPingInterval(300);

I am using sticky-service also for connection, but when I keep my application open (ideal-state) for 15-20 mins then the connection is lost, so I am using ping manger to resolve this issue.

Is there any other better way of doing it or ping manager is the only option?

like image 859
Snehangshu Kar Avatar asked Jan 28 '23 06:01

Snehangshu Kar


2 Answers

Insted of pinging chat server constantly, you better to use ConnectionListener() in smack library. You need to use something like this:

XMPPTCPConnection connection;
// initialize your connection

// handle the connection
connection.addConnectionListener(new ConnectionListener() {
      @Override 
      public void connected(XMPPConnection connection) {

      }

      @Override 
      public void authenticated(XMPPConnection connection, boolean resumed) {

      }

      @Override 
      public void connectionClosed() {
        // when the connection is closed, try to reconnect to the server.
      }

      @Override 
      public void connectionClosedOnError(Exception e) {
        // when the connection is closed, try to reconnect to the server.
      }

      @Override 
      public void reconnectionSuccessful() {

      }

      @Override 
      public void reconnectingIn(int seconds) {

      }

      @Override 
      public void reconnectionFailed(Exception e) {
        // do something here, did you want to reconnect or send the error message?
      }
    });
like image 66
ישו אוהב אותך Avatar answered Jan 31 '23 20:01

ישו אוהב אותך


Best way to keep the alive connection with XMPP server you should reconnect after every network change.

Like this:

public class NetworkStateChangeReceiver extends BroadcastReceiver {

private Context context;
private static NetworkStateChangeListener mListener;

@Override
public void onReceive(Context context, Intent intent) {

this.context = context;
try {
if (!ApplicationHelper.isInternetOn(context)) {
if (mListener != null) {
mListener.OnInternetStateOff();
}
return;
} else {
XMPPTCPConnection xmpptcpConnection = XmppConnectionHelper.getConnection();
if(!StringHelper.isNullOrEmpty(new SessionManager(context).getAuthenticationToken())) {
Intent XmppConnectionServicesIntent = new Intent(context, XmppConnectionServices.class);
context.stopService(XmppConnectionServicesIntent);
context.startService(XmppConnectionServicesIntent);
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

//to initialize NetworkStateChangeListener because null pointer exception occurred
public static void setNetworkStateChangeListener(NetworkStateChangeListener listener) {
mListener = listener;
}

}
like image 30
Shavareppa Avatar answered Jan 31 '23 18:01

Shavareppa