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?


当使用asmack在你的应用程序中放入一些类似的代码来使Dalvik加载ReconnectionManager类并运行它的静态初始化块时:

static {
    try {
        Class.forName("org.jivesoftware.smack.ReconnectionManager");
    } catch (ClassNotFoundException ex) {
        // problem loading reconnection manager
    }
}

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


I have the same problem, except that My program run on server side JVM.
I used smack 4.0 in the first place. Then I updated to smack 4.1, but the problem still happened. Finally I found a configuration module: PingManager
After using this, the occurrence of this situation was drop down.

    connection = new XMPPTCPConnection(config);     
    PingManager pingManager = PingManager.getInstanceFor(connection);
    pingManager.setPingInterval(300); // seconds
链接地址: http://www.djcxy.com/p/94082.html

上一篇: Smack API和Openfire服务器存在错误

下一篇: (a)如何在Android上保持XMPP连接稳定?