Maintaining the connection

The XmppClient object will try to maintain the connection live. This includes pinging the server and trying to reconnect if something happens. But connectivity can get lost for many reasons. The application should therefore regularly check the connection, and force a reconnection attempt if it finds it offline or in an error state. In our sensor project, we can check the connection every minute, and force it to reconnect if necessary. We can take the opportunity to use our sampling timer to achieve this:

if (Timestamp.Second == 0 && this.xmppClient != null && 
(this.xmppClient.State == XmppState.Error || 
this.xmppClient.State == XmppState.Offline)) 
{ 
this.xmppClient.Reconnect(); 
} 

In the actuator, as we don't have a sampling timer, we need to create a separate timer for this task:

this.minuteTimer = new Timer((State) => 
{ 
if (this.xmppClient != null && 
(this.xmppClient.State == XmppState.Error || 
this.xmppClient.State == XmppState.Offline)) 
{ 
this.xmppClient.Reconnect(); 
} 
}, null, 60000, 60000); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset