OnNetworkStatusChanged
Sometimes devices can loose network connection without the socket being closed. The TCP connection actually thinks that the connection is alive even though is was closed.
XSockets client API has two features to help you avoid unwanted disconnections.
- AutoHeartbeat - Ping/Ping to keep the connection alive (see section for AutoHeartbeat)
- AutoReconnect - Re-establish a connection when the connection was closed (see section for AutoReconnect)
However, the API also offers the OnNetworkChanged
event. This event will fire when a change in the network availability is detected.
Sample
A sample of how to use this can be seen below. In the sample we create a instance of the XSockets client, but we do not need to open a connection. We just listen for the OnNetworkChanged
event and write out the current status.
To trigger the event we actually just enable/disable the wifi on the machine we use.
Code
class Program
{
static void Main(string[] args)
{
var c = new XSockets.XSocketClient("ws://localhost:4502","http://localhost");
c.OnNetworkStatusChanged += (s, e) => {
Console.WriteLine("NetworkStatusChanged: {0}", e);
};
Console.ReadLine();
}
}
Output
The output from the code above will show the first line when wifi was disabled, and then the second line when wifi was enabled.