Connection lifetime events - XSockets/XSockets.NET-4.0 GitHub Wiki
##Connection lifetime events
Each controller have events that you can use to know when Open
, Close
and ReOpen
occurs.
###The OnOpen event Will fire when the controller is used over the connection for the first time.
//using XSockets.Core.XSocket;
public class Chat : XSocketController
{
public override void OnOpened()
{
//Connection to controller is open
}
}
###The OnClose event Will fire when the socket is closed or if the client choose to close this specific controller on the connection.
//using XSockets.Core.XSocket;
public class Chat : XSocketController
{
public override void OnClosed()
{
//Connection to controller was closed
}
}
###How to do heartbeats (ping/pong control-frames) Control-frames (ping/pong) is primarily for checking connections, measure latency and validate that the connection is valid.
You can implement custom logic for sending/receiving these frames on the server, but there is a simple helper that you can use to get the functionality.
//using XSockets.Core.XSocket;
public class Chat : XSocketController
{
public override void OnOpened()
{
this.ProtocolInstance.Heartbeat(notifyAction: (reason)=>{ Console.WriteLine(reason); });
}
}