Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a XMPP connection stable on Android with (a)smack?

I use asmack-android-7-beem library for Android. I have a background service running, such as my app stays alive. But sooner or later XMPP connection dies without any notice. The server says that the client is still online but no packets are sent or received.

For example the client doesn't receive any presence packets when other clients have a new presence. I have XMPPConnection as an attibute of my main Application class.
I set ConnectionConfiguration config.setReconnectionAllowed(true) before the connection was made.
But reconnection doesn't happen. XMPPConnection connection.isConnected() returns true.

So the client is not aware that connection is actually lost.

Is there any way to keep the connection alive?

like image 792
Max Avatar asked Oct 26 '12 04:10

Max


2 Answers

When using asmack put some code like this in your app to make Dalvik load the ReconnectionManager class and run it's static initialization block:

static {     try {         Class.forName("org.jivesoftware.smack.ReconnectionManager");     } catch (ClassNotFoundException ex) {         // problem loading reconnection manager     } } 
like image 158
bradlaronde Avatar answered Sep 19 '22 06:09

bradlaronde


Actually There is not any problem with Reconnection manager. First you need to add connection listener to your connection manager.

connection.addConnectionListener(new ConnectionListener() {                      @Override                     public void reconnectionSuccessful() {                         Log.i("","Successfully reconnected to the XMPP server.");                      }                      @Override                     public void reconnectionFailed(Exception arg0) {                         Log.i("","Failed to reconnect to the XMPP server.");                     }                      @Override                     public void reconnectingIn(int seconds) {                         Log.i("","Reconnecting in " + seconds + " seconds.");                     }                      @Override                     public void connectionClosedOnError(Exception arg0) {                         Log.i("","Connection to XMPP server was lost.");                     }                      @Override                     public void connectionClosed() {                         Log.i("","XMPP connection was closed.");                      }                 });  

if any error occurred the connectionClosedOnError(Exception arg0) will automatically called when connection is closed

 public void connectionClosed() {                         Log.i("","XMPP connection was closed.");                         //You can manually call reconnection code if you                  want to reconnect on any connection close                     } 

then check it this will call reconnectingin() method and try to reconnect.

Hope so this will help you.

use below code for check connection PingManager pingManager = PingManager.getInstanceFor(connection); pingManager.setPingInterval(5000);

add listner for ping fail handling to handle connection is connected or not because isConnected method is not reliable for check state of connection.

pingManager.registerPingFailedListener(PingFailedListener);

For mobile network connectivity is very big problem so you need to check network connectivity for mobile using broadcast receiver and on data reconnection you can use pingMyServer method to check connection is alive or not, if you are getting ping reply from server, means connection is alive otherwise on ping fail you can reconnect connection manually.

like image 26
Mandeep Avatar answered Sep 19 '22 06:09

Mandeep