A concrete implementation of the .Net Standard 2.0 System.Net.WebSockets.WebSocket abstract class
A WebSocket library that allows you to make WebSocket connections as a client or to respond to WebSocket requests as a server. You can safely pass around a general purpose WebSocket instance throughout your codebase without tying yourself strongly to this library. This is the same WebSocket abstract class used by .net core 2.0 and it allows for asynchronous Websocket communication for improved performance and scalability.
No dependencies.
As a client, use the WebSocketClientFactory
var factory = new WebSocketClientFactory();
WebSocket webSocket = await factory.ConnectAsync(new Uri("wss://example.com"));
As a server, use the WebSocketServerFactory
Stream stream = tcpClient.GetStream();
var factory = new WebSocketServerFactory();
WebSocketHttpContext context = await factory.ReadHttpHeaderFromStreamAsync(stream);
if (context.IsWebSocketRequest)
{
WebSocket webSocket = await factory.AcceptWebSocketAsync(context);
}
Tests are written for xUnit
David Haig
This project is licensed under the MIT License - see the LICENSE.md file for details
-
Step by step guide: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
-
The official WebSocket spec: http://tools.ietf.org/html/rfc6455
This library is based on all the amazing feedback I got after writing this article (thanks all): https://www.codeproject.com/articles/1063910/websocket-server-in-csharp
The code in the article above was written before Microsoft made System.Net.WebSockets.WebSocket generally available with .NetStandard 2.0 but the concepts remain the same. Take a look if you are interested in the inner workings of the websocket protocol.