Skip to content

Enabling WebSocket Secure (TLS)

vtortola edited this page Mar 20, 2014 · 9 revisions

The WSS support is provided through a custom connection extension named WebSocketSecureConnectionExtension.

It requires a certificate object, that will be used to secure the connection:

server.ConnectionExtensions.RegisterExtension(new WebSocketSecureConnectionExtension(certificate)); 

When using TLS, the clients will need to use the wss:// schema to connect.

How to obtain that certificate object is up to the caller, but this would be a little example:

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
store.Certificates.Count.ToString();
var certificate = store.Certificates[1];
store.Close();

When using TLS, is recommended to increment the number of available parallel negotiations through the WebSocketListener options since TLS negotiation takes a little bit longer:

var options = new WebSocketListenerOptions() 
{ 
   NegotiationQueueCapacity = 128, 
   ParallelNegotiations = 16 
}

WebSocketListener server = new WebSocketListener(endpoint, options);
server.ConnectionExtensions.RegisterExtension(new WebSocketSecureConnectionExtension(certificate));

Tune the option values to find the config that works out better for you.

Clone this wiki locally