ReconnectionManager无法正常工作

时间:2016-06-06 13:14:09

标签: android xmpp openfire smack

我正在使用smack库在android中实现聊天解决方案。它完全与openfire连接,但重新连接根本不起作用。

我在设备上丢失了连接,在连接接收时它没有重新连接或调用任何重新连接的回调。请建议我们如何使用ReconnectManager重新连接到openfire。

private boolean openConnection(String account, String password) {
    try {
        if (null == xmppConnection || !xmppConnection.isAuthenticated()) {
            XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
            config.setHost(Constants.XMPP_SERVER_DOMAIN);
            config.setServiceName(Constants.XMPP_SERVER_DOMAIN);
            config.setPort(Constants.XMPP_SERVER_PORT);
            config.setUsernameAndPassword(account, password);
            //config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setCompressionEnabled(true);
            config.setDebuggerEnabled(true);

            TLSUtils.acceptAllCertificates(config);

            xmppConnection = new XMPPTCPConnection(config.build());
            xmppConnection.setPacketReplyTimeout(10000);
            ReconnectionManager.getInstanceFor(xmppConnection).enableAutomaticReconnection();
            connectionListener = new XmppConnectionListener();
            packetListener = new XmppPacketListener();

            xmppConnection.addAsyncPacketListener(packetListener, new PacketFilter() {
                @Override
                public boolean accept(Stanza packet) {
                    return true;
                }
            });

            xmppConnection.addConnectionListener(connectionListener);
            xmppConnection.connect().login();
            return true;
        }
    } catch (XMPPException xe) {
        xe.printStackTrace();
        xmppConnection = null;
    } catch (IOException io) {
        io.printStackTrace();
        xmppConnection = null;
    } catch (SmackException se) {
        se.printStackTrace();
        xmppConnection = null;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        xmppConnection = null;
    } catch (KeyManagementException e) {
        e.printStackTrace();
        xmppConnection = null;
    }
    return false;
}

XmppConnectionListener.java

public class XmppConnectionListener implements ConnectionListener {

     private static final String TAG = "XmppConnectionListener";

     @Override
     public void reconnectionSuccessful() {
         Log.d(TAG, "reconnectionSuccessful()");
     }

     @Override
     public void reconnectionFailed(Exception arg0) {
         Log.d(TAG, "reconnectionFailed()");
     }

     @Override
     public void reconnectingIn(int arg0) {
         Log.d(TAG, "reconnectingIn()");
     }

     @Override
     public void connectionClosedOnError(Exception arg0) {
         Log.d(TAG, "connectionClosedOnError()");
     }

     @Override
     public void connected(XMPPConnection connection) {
         Log.d(TAG, "connected()");
     }

     @Override
     public void authenticated(XMPPConnection connection, boolean resumed) {
         Log.d(TAG, "authenticated()");

     }

     @Override
     public void connectionClosed() {
         Log.d(TAG, "connectionClosed()");
     }
 }

0 个答案:

没有答案