WebSocketListener Extensions

vtortola edited this page May 5, 2014 · 6 revisions

There are two extensibility points:

  • Per Connection extension: Allows to wrap the underlying connection stream. The TLS extension is a connection extension. These extensions are shared for all the WebSocket standards.
  • Per Message extension: Allows to wrap the message stream. The Deflate compression extension is a message extension. These extensions are added to each [standard] (//github.com/vtortola/WebSocketListener/wiki/Multiple-WebSocket-standard-support).

Per Connection Extensions

Needs to fulfill the IWebSocketConnectionExtension contract:

    public interface IWebSocketConnectionExtension
        Stream ExtendConnection(Stream stream);
        Task<Stream> ExtendConnectionAsync(Stream stream);

And it need to be added to the WebSocketListener before start.

server.ConnectionExtensions.RegisterExtension(new MyConnectionExtension());

Per Message Extensions

Needs to fulfill the IWebSocketMessageExtension contract:

    public interface IWebSocketMessageExtension
        String Name { get;}
        Boolean TryNegotiate(WebSocketHttpRequest request, out WebSocketExtension extensionResponse, out IWebSocketMessageExtensionContext context);

The WebSocketExtension out object, will contain the result of the extension negotiation, and it will be returned to the client as part of the Sec-WebSocket-Extensions HTTP header.

The IWebSocketMessageExtensionContext contract represents an object that will be unique per WebSocket connection that will be used for wrapping the messages.

    public interface IWebSocketMessageExtensionContext
        WebSocketMessageReadStream ExtendReader(WebSocketMessageReadStream message);
        WebSocketMessageWriteStream ExtendWriter(WebSocketMessageWriteStream message);

And it need to be added to the WebSocketListener before start.

var rfc6455 = new vtortola.WebSockets.Rfc6455.WebSocketFactoryRfc6455(server);
rfc6455.MessageExtensions.RegisterExtension(new MyMessageExtension());