I am using Smack and Openfire server for a chat client, all things working well like chat, sending an invitation for a new addition of user, getting list of available users etc. I don't have a clue what to do if the connection is in a Sticky Service and I added a connection listener to the connection and connection disconnected, let's say for "Internet Connection"
I am using the below code for the connectionlistener.
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.");
Log.v("ONMESSAGE", "Error was " + arg0.toString() + "and Now connecting");
}
@Override
public void connectionClosed() {
Log.i("","XMPP connection was closed.");
}
});
So I thought of adding two lines of code to connectionClosedOnError() i.e
connection.disconnect(new Presence(Presence.Type.unavailable));
//code for connection(new one)
Which gives me some time following error
- No Response from Server.
- Not connected to server.
- conflict error
Now I researched on the issue and found there is still connection when I try to reconnect using the same resource so I got the errors. My question is how to do reconnection and what is the correct procedure for that?
I know how to resolve the issue "XMPP “stream:error (conflict)” as I can provide a String to login() method as a third parameter and it resolves the issue.
That is not my main concern, what I want to know the procedure of reconnection. I tried logging in all methods and it's quite amazing that there are no order of methods being called.