Team XSockets.NET

Heartbeats (ping/pong) (deprecated since 5.6.7)

Since version 5.6.7 the heartbeat can be configured per endpoint and there is no need to use heartbeat functionality from the controller. It is now on protocol-level.


Ping/Pong may not be supported by all clients and protocols. For example Microsoft did not implement the ping/pong in their WebSocket implementation for Owin.

Heartbeats can work in many different ways, but often the server sends a ping and the client responds with a pong. The C# client and JavaScript client of XSockets has this functionality implemented. If you build a custom protocol (and/or custom clients) you will have to implement this your self.

If you look at the reserved topics you can see that there is reservations for ping/pong. This was added since Microsofts websocket implementation in Owin does not follow the RFC6455 (websocket protocol) specification.

Automatic heartbeat

Even though it is easy to build your own heartbeat there is functionality built-in for this. On the protocol instance you can call the Heartbeat method to start the heartbeat. This will send a ping to the client every 30 second (configurable) by default.

Below you can see that we initiate the heartbeat when the controller is opened. If/When the heartbeat fails the callback notifyAction will be invoked with the reason for the failure. The reason will be either timeout or invalid

public class Foo : XSocketController
{        
    public override Task OnOpened()
    {
        this.ProtocolInstance.Heartbeat(notifyAction: (reason)=> {
            Composable.GetExport<IXLogger>().Information("Connection broken due to {r}", reason);
        });
        return base.OnOpened();
    }
}

If you use the C# client from XSockets or the JavaScript client from XSockets the heartbeat response will be built-in. If you use any other client you will have to handle this your self.

results matching ""

    No results matching ""