WebSocketChannel 0.9.0

There is a newer version of this package available.
See the version list below for details.
Install-Package WebSocketChannel -Version 0.9.0
dotnet add package WebSocketChannel --version 0.9.0
<PackageReference Include="WebSocketChannel" Version="0.9.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WebSocketChannel --version 0.9.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WebSocketChannel, 0.9.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install WebSocketChannel as a Cake Addin
#addin nuget:?package=WebSocketChannel&version=0.9.0

// Install WebSocketChannel as a Cake Tool
#tool nuget:?package=WebSocketChannel&version=0.9.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Version Downloads License Build

Usage

var client = new ClientWebSocket();
await client.ConnectAsync(serverUri, CancellationToken.None);

Channel<ReadOnlyMemory<byte>> channel = client.CreateChannel();

await channel.Writer.WriteAsync(Encoding.UTF8.GetBytes("hello").AsMemory());

// Read single message when it arrives
ReadOnlyMemory<byte> response = await channel.Reader.ReadAsync();

// Read all messages while underlying websocket is open
await foreach (var item in channel.Reader.ReadAllAsync())
{
    Console.WriteLine(Encoding.UTF8.GetString(item.Span));
}

// Completing the writer closes the underlying websocket cleanly
channel.Writer.Complete();

// Can also complete reporting an error for the remote party
channel.Writer.Complete(new InvalidOperationException("Bad format"));

The WebSocketChannel can also be used on the server. The following example is basically taken from the documentation on WebSockets in ASP.NET Core and adapted to use a WebSocketChannel to echo messages to the client:

app.Use(async (context, next) =>
{
    if (context.Request.Path == "/ws")
    {
        if (context.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
            var channel = WebSocketChannel.Create(webSocket);
            try
            {
                await foreach (var item in channel.Reader.ReadAllAsync(context.RequestAborted))
                {
                    await channel.Writer.WriteAsync(item, context.RequestAborted);
                }
            }
            catch (OperationCanceledException)
            {
                await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default);
            }
        }
        else
        {
            context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
        }
    }
    else
    {
        await next();
    }
});

Sponsors

sponsored clariusclarius

get mentioned here too!

NuGet packages (1)

Showing the top 1 NuGet packages that depend on WebSocketChannel:

Package Downloads
WebSocketeer

A thin, intuitive, idiomatic and high-performance layer for Azure Web PubSub protobuf subprotocol. Built from https://github.com/devlooped/WebSocketeer/tree/c4da4fc4b

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.2 262 10/15/2021
0.9.1 192 10/4/2021
0.9.0 133 10/4/2021