|
| 1 | + WebSocketChannel |
| 2 | +============ |
| 3 | + |
| 4 | +High-performance [System.Threading.Channels](https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/) API adapter for System.Net.WebSockets |
| 5 | + |
| 6 | +[](https://www.nuget.org/packages/WebSocketChannel) |
| 7 | +[](https://www.nuget.org/packages/WebSocketChannel) |
| 8 | +[](https://github.com/devlooped/WebSocketChannel/blob/main/license.txt) |
| 9 | +[](https://github.com/devlooped/WebSocketChannel/actions) |
| 10 | + |
| 11 | +# Usage |
| 12 | + |
| 13 | +```csharp |
| 14 | +var client = new ClientWebSocket(); |
| 15 | +await client.ConnectAsync(serverUri, CancellationToken.None); |
| 16 | + |
| 17 | +Channel<ReadOnlyMemory<byte>> channel = client.CreateChannel(); |
| 18 | + |
| 19 | +await channel.Writer.WriteAsync(Encoding.UTF8.GetBytes("hello").AsMemory()); |
| 20 | + |
| 21 | +// Read single message when it arrives |
| 22 | +ReadOnlyMemory<byte> response = await channel.Reader.ReadAsync(); |
| 23 | + |
| 24 | +// Read all messages while underlying websocket is open |
| 25 | +await foreach (var item in channel.Reader.ReadAllAsync()) |
| 26 | +{ |
| 27 | + Console.WriteLine(Encoding.UTF8.GetString(item.Span)); |
| 28 | +} |
| 29 | + |
| 30 | +// Completing the writer closes the underlying websocket cleanly |
| 31 | +channel.Writer.Complete(); |
| 32 | + |
| 33 | +// Can also complete reporting an error for the remote party |
| 34 | +channel.Writer.Complete(new InvalidOperationException("Bad format")); |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +The `WebSocketChannel` can also be used on the server. The following example is basically |
| 39 | +taken from the documentation on [WebSockets in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-5.0#configure-the-middleware) |
| 40 | +and adapted to use a `WebSocketChannel` to echo messages to the client: |
| 41 | + |
| 42 | +```csharp |
| 43 | +app.Use(async (context, next) => |
| 44 | +{ |
| 45 | + if (context.Request.Path == "/ws") |
| 46 | + { |
| 47 | + if (context.WebSockets.IsWebSocketRequest) |
| 48 | + { |
| 49 | + using var webSocket = await context.WebSockets.AcceptWebSocketAsync(); |
| 50 | + var channel = WebSocketChannel.Create(webSocket); |
| 51 | + try |
| 52 | + { |
| 53 | + await foreach (var item in channel.Reader.ReadAllAsync(context.RequestAborted)) |
| 54 | + { |
| 55 | + await channel.Writer.WriteAsync(item, context.RequestAborted); |
| 56 | + } |
| 57 | + } |
| 58 | + catch (OperationCanceledException) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default); |
| 63 | + } |
| 64 | + catch { } // Best effort to try closing cleanly. Client may be entirely gone. |
| 65 | + } |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + context.Response.StatusCode = (int) HttpStatusCode.BadRequest; |
| 70 | + } |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + await next(); |
| 75 | + } |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | +# Dogfooding |
| 81 | + |
| 82 | +[](https://pkg.kzu.io/index.json) |
| 83 | +[](https://github.com/devlooped/WebSocketChannel/actions) |
| 84 | + |
| 85 | +We also produce CI packages from branches and pull requests so you can dogfood builds as quickly as they are produced. |
| 86 | + |
| 87 | +The CI feed is `https://pkg.kzu.io/index.json`. |
| 88 | + |
| 89 | +The versioning scheme for packages is: |
| 90 | + |
| 91 | +- PR builds: *42.42.42-pr*`[NUMBER]` |
| 92 | +- Branch builds: *42.42.42-*`[BRANCH]`.`[COMMITS]` |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +## Sponsors |
| 97 | + |
| 98 | +[](https://github.com/sponsors/devlooped) [](https://github.com/clarius)[](https://github.com/clarius) |
| 99 | + |
| 100 | +*[get mentioned here too](https://github.com/sponsors/devlooped)!* |
0 commit comments