Maintain XMPP connection

Hello everyone,

New with Smack, I’ve encapsulated the creation of my xmpp connection into the following function:

public AbstractXMPPConnection createConnection() {

SmackConfiguration.DEBUG = true;

AbstractXMPPConnection conn;

// build connection
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();

configBuilder.setServiceName(P2PConstants.P2P_CONF_DOMAIN);

configBuilder.setHost(P2PConstants.P2P_CONF_SERVER);

configBuilder.setPort(P2PConstants.P2P_CONF_PORT);

configBuilder.setCustomSSLContext(createSSLContext());

configBuilder.setHostnameVerifier(new P2PHostnameVerifier());

configBuilder.setResource("");

conn = new XMPPTCPConnection(configBuilder.build());

// set reconnection policy
ReconnectionManager connMgr = ReconnectionManager.getInstanceFor(conn);

if (P2PConstants.P2P_RECONNECTION_ENABLED)

connMgr.enableAutomaticReconnection();

else
connMgr.disableAutomaticReconnection();

connMgr.setReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY);

connMgr.setFixedDelay(P2PConstants.P2P_RECONNECTION_DELAY_S);

// set reconnection default policy
ReconnectionManager.setEnabledPerDefault(P2PConstants.P2P_RECONNECTION_ENABLED);

ReconnectionManager.setDefaultReconnectionPolicy(ReconnectionManager.ReconnectionPolicy.FIXED_DELAY);

ReconnectionManager.setDefaultFixedDelay(P2PConstants.P2P_RECONNECTION_DELAY_S);

// set ping interval
PingManager pingManager = PingManager.getInstanceFor(conn);

pingManager.setPingInterval(120);

// set default ping interval
PingManager.setDefaultPingInterval(120);

return conn;

}

At application level, I register a connectionListener to monitor the connection.

Two questions now:

  • What is the best way to maintain the connection ?
    • register a connectionListener and explicitely ask for reconnection when connection has been lost ?
    • let the reconnection manager
  • When running my android app while connected and authenticated, if a deactivate the network, nothing happens. The socket is probably closed but Smack does not see it…

Thanks a lot for your support on your so helpful library,

Pierre

I also notice the connection closed callbacks are not always responding as they should, I have a Log print out in each case which is not called when the connection is dropped.