Skip to content

Repository files navigation

Ninja WebSockets

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.

Dependencies

No dependencies.

Getting Started

As a client, use the WebSocketClientFactory

varfactory=newWebSocketClientFactory();WebSocketwebSocket=awaitfactory.ConnectAsync(newUri("wss://example.com"));

As a server, use the WebSocketServerFactory

Streamstream=tcpClient.GetStream();varfactory=newWebSocketServerFactory();WebSocketHttpContextcontext=awaitfactory.ReadHttpHeaderFromStreamAsync(stream);if(context.IsWebSocketRequest){WebSocketwebSocket=awaitfactory.AcceptWebSocketAsync(context);}

Using the WebSocket class

Client and Server send and receive data the same way.

Receiving Data:

privateasyncTaskReceive(WebSocketwebSocket){varbuffer=newArraySegment<byte>(newbyte[1024]);while(true){WebSocketReceiveResultresult=awaitwebSocket.ReceiveAsync(buffer,CancellationToken.None);switch(result.MessageType){caseWebSocketMessageType.Close:return;caseWebSocketMessageType.Text:caseWebSocketMessageType.Binary:stringvalue=Encoding.UTF8.GetString(buffer.Array,0,result.Count);Console.WriteLine(value);break;}}}

Receive data in an infinite loop until we receive a close frame from the server.

Sending Data:

privateasyncTaskSend(WebSocketwebSocket){vararray=Encoding.UTF8.GetBytes("Hello World");varbuffer=newArraySegment<byte>(array);awaitwebSocket.SendAsync(buffer,WebSocketMessageType.Text,true,CancellationToken.None);}

Simple client Request / Response: The best approach to communicating using a web socket is to send and receive data on different worker threads as shown below.

publicasyncTaskRun(){varfactory=newWebSocketClientFactory();varuri=newUri("ws://localhost:27416/chat");using(WebSocketwebSocket=awaitfactory.ConnectAsync(uri)){// receive loopTaskreadTask=Receive(webSocket);// send a messageawaitSend(webSocket);// initiate the close handshakeawaitwebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure,null,CancellationToken.None);// wait for server to respond with a close frameawaitreadTask;}}

WebSocket Extensions

Websocket extensions like compression (per message deflate) is currently work in progress.

Running the tests

Tests are written for xUnit

Authors

David Haig

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

Further Reading

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.

About

A c# implementation of System.Net.WebSockets.WebSocket for .Net Standard 2.0

Resources

Stars

172 stars

Watchers

20 watching

Forks

Releases

Packages

Used by

Contributors

Languages