KbWebSocketServer 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package KbWebSocketServer --version 0.2.0                
NuGet\Install-Package KbWebSocketServer -Version 0.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KbWebSocketServer" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KbWebSocketServer --version 0.2.0                
#r "nuget: KbWebSocketServer, 0.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install KbWebSocketServer as a Cake Addin
#addin nuget:?package=KbWebSocketServer&version=0.2.0

// Install KbWebSocketServer as a Cake Tool
#tool nuget:?package=KbWebSocketServer&version=0.2.0                

NuGet

KbWebSocketServer

KbWebSocketServer is a lightweight .net websocket server library.

  • Simple Api interface
  • No 3rd-party package dependency required
  • Target framework: net5.0;net6.0;net7.0

Usage Tutorials

Install package

Install-Package KbWebSocketServer

Or, if you don't want to add another package to your project, you can just copy all source files into your codebase. KbWebSocketServer is a clean and lightweight project, it doesn't require any additional package but only .net coreclr library itself.

Start up a websocket server and accept client requests

// specify server listening IP and port.
var wss = new WebSocketServer(8000);

// start up server, pass in a client request handler.
wss.Start(async ctx => 
{
    // accept this client request.
    var ws = await ctx.AcceptWebSocketAsync();
    Write(ws);
});

async ValueTask Write(WebSocket ws)
{
    await foreach (var message in ws.ReceiveMessagesAsync())
    {
        if (message.MessageType == WebSocketMessageType.Text)
        {
            Console.WriteLine(message.Text.ToString());
        }
    }
}

Reject client requests

var wss = new WebSocketServer(8000);

wss.Start(async ctx => 
{
    // set an error status code.
    ctx.ResponseStatusCode = HttpStatusCode.Unauthorized;
    // put some error information in response headers,
    // to allow client getting error details from server response.
    //
    // it's optional, but useful.
    ctx.ResponseHeaders.Add("x-custom-error", "You shall not pass!");
});

Receive/send messages from/to a client

var wss = new WebSocketServer(8000);

wss.Start(async ctx => 
{
    var ws = await ctx.AcceptWebSocketAsync();
    Echo(ws);
});

async ValueTask Echo(WebSocket ws)
{
    // ReceiveMessagesAsync() returns an IAsyncEnumerable,
    // which can be consumed by `await foreach`.
    await foreach (var message in ws.ReceiveMessagesAsync())
    {
        // message type can be `Text` or `Binary`.
        if (message.MessageType == WebSocketMessageType.Text)
        {
            // send back a text message.
            await ws.SendTextAsync($"Reply: {message.Text}");
        }
    }
}

Broadcast to all clients

var wss = new WebSocketServer(8000);

var clients = ImmutableArray<WebSocket>.Empty;

wss.Start(async ctx =>
{
    var ws = await ctx.AcceptWebSocketAsync();
    // put new client into a collection.
    ImmutableInterlocked.Update(
        ref clients,
        arr => arr.Add(ws));
    Pub(ws);
});

async ValueTask Pub(WebSocket ws)
{
    var textMessages = ws
        .ReceiveMessagesAsync()
        .Where(msg => msg.MessageType == WebSocketMessageType.Text);
    
    await foreach (var message in textMessages)
    {
        // broadcast message to all opened clients.
        foreach (var client in clients)
        {
            if (client.State == WebSocketState.Open)
            {
                await client.SendTextAsync($"Pub: {message.Text}");
            }
        }
    }
}

Determine client disconnects

var wss = new WebSocketServer(8000);

wss.Start(async ctx => 
{
    var ws = await ctx.AcceptWebSocketAsync();
    Echo(ws);
});

async ValueTask Echo(WebSocket ws)
{
    var textMessages = ws
        .ReceiveMessagesAsync()
        .Where(msg => msg.MessageType == WebSocketMessageType.Text);

    // when this client is disconnected,
    // it'll break the `await foreach` loop.
    await foreach (var message in textMessages)
    {
        // ...
    }

    if (ws.State != WebSocketState.Open)
    {
        Console.WriteLine($"a client is disconnected. state={ws.State}");
    }
}

Close client connection from server

var wss = new WebSocketServer(8000);

wss.Start(async ctx => 
{
    var ws = await ctx.AcceptWebSocketAsync();
    Echo(ws);
});

async ValueTask Echo(WebSocket ws)
{
    var textMessages = ws
        .ReceiveMessagesAsync()
        .Where(msg => msg.MessageType == WebSocketMessageType.Text);

    await foreach (var message in textMessages)
    {
        if (message.Text.ToString().Equals("PleaseClose", StringComparison.OrdinalIgnoreCase))
        {
            // close client connection.
            await ws.CloseAsync(
                WebSocketCloseStatus.NormalClosure, 
                "Closed by server.", 
                CancellationToken.None);
            break;
        }
    }

    if (ws.State != WebSocketState.Open)
    {
        Console.WriteLine($"a client is disconnected. state={ws.State}");
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.3.0 447 9/15/2022
0.2.0 444 9/14/2022
0.1.0-alpha 175 4/15/2022