Subject: Handling disconnections
I have developed a bot which has to be online 24/24. Sometimes disconnections happened, and I would like to know the best ways to handle that.
For example, I see in my logs:
The first message is logged in the "onXmppConnectionStateChanged" event handler, you can see that my bot is disconnected.
The second message ("Closed!! Re-opening...") is logged in the "OnClose" event handler. Here I re-open the connection (by calling XmppClientConnection.Open) ; is it correct to do that?
Then you can see that the bot is trying to reconnect but there are socket exceptions, so it is disconnected again and a new connection attempt is made.
This loop is repeated until the connection succeeds (one hour later):
But in this case the bot doesn't appear online, because some events are missing, here is the correct sequence:
I hope my explanations are understandable
So what is the best way to handle disconnections and provide automatic reconnections?
Thanks in advance
For example, I see in my logs:
2007-05-04 23:49:21,745 ERROR [Core] - onXmppConnectionStateChanged : Disconnected
2007-05-04 23:49:21,761 DEBUG [Core] - Closed!! Re-opening...
2007-05-04 23:49:21,761 ERROR [Core] - onXmppConnectionStateChanged : Connecting
2007-05-04 23:49:23,527 ERROR [Core] - onError : System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at agsXMPP.net.ClientSocket.EndConnect(IAsyncResult ar)
2007-05-04 23:49:23,527 ERROR [Core] - onSocketError : System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at agsXMPP.net.ClientSocket.EndConnect(IAsyncResult ar)
2007-05-04 23:49:23,542 ERROR [Core] - onXmppConnectionStateChanged : Disconnected
2007-05-04 23:49:23,542 DEBUG [Core] - Closed!! Re-opening...
etc.etc.
2007-05-04 23:49:21,761 DEBUG [Core] - Closed!! Re-opening...
2007-05-04 23:49:21,761 ERROR [Core] - onXmppConnectionStateChanged : Connecting
2007-05-04 23:49:23,527 ERROR [Core] - onError : System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at agsXMPP.net.ClientSocket.EndConnect(IAsyncResult ar)
2007-05-04 23:49:23,527 ERROR [Core] - onSocketError : System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at agsXMPP.net.ClientSocket.EndConnect(IAsyncResult ar)
2007-05-04 23:49:23,542 ERROR [Core] - onXmppConnectionStateChanged : Disconnected
2007-05-04 23:49:23,542 DEBUG [Core] - Closed!! Re-opening...
etc.etc.
The first message is logged in the "onXmppConnectionStateChanged" event handler, you can see that my bot is disconnected.
The second message ("Closed!! Re-opening...") is logged in the "OnClose" event handler. Here I re-open the connection (by calling XmppClientConnection.Open) ; is it correct to do that?
Then you can see that the bot is trying to reconnect but there are socket exceptions, so it is disconnected again and a new connection attempt is made.
This loop is repeated until the connection succeeds (one hour later):
2007-05-05 00:47:29,610 ERROR [Core] - onXmppConnectionStateChanged : Disconnected
2007-05-05 00:47:29,610 DEBUG [Core] - Closed!! Re-opening...
2007-05-05 00:47:29,610 ERROR [Core] - onXmppConnectionStateChanged : Connecting
2007-05-05 00:47:30,485 ERROR [Core] - onXmppConnectionStateChanged : Connected
2007-05-05 00:47:29,610 DEBUG [Core] - Closed!! Re-opening...
2007-05-05 00:47:29,610 ERROR [Core] - onXmppConnectionStateChanged : Connecting
2007-05-05 00:47:30,485 ERROR [Core] - onXmppConnectionStateChanged : Connected
But in this case the bot doesn't appear online, because some events are missing, here is the correct sequence:
2007-05-08 17:46:17,953 ERROR [Core] - onXmppConnectionStateChanged : Connecting
2007-05-08 17:46:19,031 ERROR [Core] - onXmppConnectionStateChanged : Connected
2007-05-08 17:46:19,406 ERROR [Core] - onXmppConnectionStateChanged : Securing
2007-05-08 17:46:19,734 ERROR [Core] - onXmppConnectionStateChanged : Authenticating
2007-05-08 17:46:22,000 ERROR [Core] - onXmppConnectionStateChanged : Authenticated
2007-05-08 17:46:22,312 ERROR [Core] - onXmppConnectionStateChanged : Binding
2007-05-08 17:46:22,703 ERROR [Core] - onXmppConnectionStateChanged : Binded
2007-05-08 17:46:22,703 ERROR [Core] - onXmppConnectionStateChanged : StartSession
2007-05-08 17:46:23,015 ERROR [Core] - onXmppConnectionStateChanged : SessionStarted
2007-05-08 17:46:23,015 DEBUG [Core] - Logged in.
2007-05-08 17:46:19,031 ERROR [Core] - onXmppConnectionStateChanged : Connected
2007-05-08 17:46:19,406 ERROR [Core] - onXmppConnectionStateChanged : Securing
2007-05-08 17:46:19,734 ERROR [Core] - onXmppConnectionStateChanged : Authenticating
2007-05-08 17:46:22,000 ERROR [Core] - onXmppConnectionStateChanged : Authenticated
2007-05-08 17:46:22,312 ERROR [Core] - onXmppConnectionStateChanged : Binding
2007-05-08 17:46:22,703 ERROR [Core] - onXmppConnectionStateChanged : Binded
2007-05-08 17:46:22,703 ERROR [Core] - onXmppConnectionStateChanged : StartSession
2007-05-08 17:46:23,015 ERROR [Core] - onXmppConnectionStateChanged : SessionStarted
2007-05-08 17:46:23,015 DEBUG [Core] - Logged in.
I hope my explanations are understandable

So what is the best way to handle disconnections and provide automatic reconnections?
Thanks in advance