LHZ.WebSocket.AspNetCore 1.1.1

dotnet add package LHZ.WebSocket.AspNetCore --version 1.1.1
                    
NuGet\Install-Package LHZ.WebSocket.AspNetCore -Version 1.1.1
                    
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="LHZ.WebSocket.AspNetCore" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LHZ.WebSocket.AspNetCore" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="LHZ.WebSocket.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LHZ.WebSocket.AspNetCore --version 1.1.1
                    
#r "nuget: LHZ.WebSocket.AspNetCore, 1.1.1"
                    
#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.
#:package LHZ.WebSocket.AspNetCore@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LHZ.WebSocket.AspNetCore&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=LHZ.WebSocket.AspNetCore&version=1.1.1
                    
Install as a Cake Tool

LHZ.WebSocket.AspNetCore

English | 中文

A lightweight ASP.NET Core middleware that integrates the LHZ.WebSocket library to handle HTTP-to-WebSocket upgrades and manage active WebSocket clients.

Overview

This library exposes a minimal middleware extension UseWebSocket for ASP.NET Core applications. It wraps LHZ.WebSocket primitives and provides an IHttpContext abstraction so applications can accept WebSocket upgrades, create WebSocketClient instances, and manage client lifecycles.

Features

  • UseWebSocket middleware for handling WebSocket upgrade requests
  • IHttpContext wrapper that performs the RFC6455 handshake (with Sec-WebSocket-Key / Sec-WebSocket-Version validation) and returns a WebSocketClient
  • Synchronous and asynchronous upgrade APIs: HttpUpgrade / HttpUpgradeAsync
  • Thread-safe registration and removal of connected clients
  • Optional upgrade timeout control

Requirements

  • .NET 5 / 6 / 8 / 9 / 10
  • LHZ.WebSocket package (version 1.1.1)

Installation

Install from NuGet:

dotnet add package LHZ.WebSocket.AspNetCore

Usage

Register the middleware in the ASP.NET Core pipeline. The delegate receives an IHttpContext for the upgrade request.

using System;
using LHZ.WebSocket.AspNetCore;
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseWebSocket(async context =>
{
    // Perform the handshake and obtain a WebSocket client
    var client = await context.HttpUpgradeAsync();

    client.OnMessageReceived += (c, message) => c.SendMessage($"Echo: {message}");
    client.OnCloseRecived += (c, reason) => c.Close();
});

app.Run();

Notes:

  • The delegate is invoked only when the request carries a WebSocket Upgrade header (matched case-insensitively per RFC 7230).
  • Call context.HttpUpgrade() or await context.HttpUpgradeAsync() to perform the handshake and create a WebSocketClient. Invalid handshake requests (missing Sec-WebSocket-Key or a version other than 13) are rejected with a 400 Bad Request response.
  • Use app.GetWebSocketClientCount() and app.GetWebSocketClients() to inspect active clients.

API Summary

  • UseWebSocket(WebSocketUpgradeDelegate webSocketUpgradeDelegate, int timeOut = 10) — Adds middleware to handle upgrades; timeOut limits seconds to wait for an upgrade.
  • UseWebSocket(Func<IHttpContext, Task> webSocketUpgradeDelegate, int timeOut = 10) — Async overload of the same middleware.
  • GetWebSocketClients() — Returns active WebSocketClient instances for the IApplicationBuilder.
  • GetWebSocketClientCount() — Returns the number of active clients for the IApplicationBuilder.

Example

See src/LHZ.WebSocket.AspNetCore.Console/Program.cs for a runnable example that logs messages and client counts.

Development

Build the solution:

dotnet build src/LHZ.WebSocket.AspNetCore.slnx

Run tests:

dotnet test src/LHZ.WebSocket.AspNetCore.Test/LHZ.WebSocket.AspNetCore.Test.csproj

Contributing

Contributions and issues are welcome. Please open pull requests or issues at the repository URL.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.1.1 102 8/19/2026
1.0.1 111 8/3/2026
1.0.0 102 8/3/2026

1.1.1:Fix invalid TargetFrameworks (net5.0-net10.0) so the package builds again; add async UseWebSocket(Func<IHttpContext, Task>) overload and HttpUpgradeAsync; validate Sec-WebSocket-Key/Sec-WebSocket-Version and reject invalid handshakes with 400; match Upgrade header case-insensitively; store clients per application instead of a static dictionary; add Kestrel integration tests