AutoHeartbeat
You can easily create your own ping/pong mechanism for heartbeats, but we have added one for you that you can use if you do not have any specific needs.
Just enable the AutoHeartbeat
by calling the SetAutoHeartbeat
method. By default the ping timeout is 30 seconds, but the method let you override this value.
Code
The code below will open a connection and let the client ping the server every 30 second. If there is no pong received from the server the connection is assumed to be dead and the client will close the connection.
var c = new XSockets.XSocketClient("ws://localhost:4502", "http://localhost", "generic");
c.OnPong += (s, e) => { Console.WriteLine("Server responded with pong {0}", DateTime.Now); };
c.SetAutoHeartbeat();
var r = await c.Open();
Console.WriteLine("Open: {0}",r);
Output
The output shows the result from the call to Open()
. True since the connection was successful. This client will then ping every 30 second, and the server will respond with a pong. We listen to the pong event in the client to show that we get the pong back.