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
<PackageReference Include="LHZ.WebSocket.AspNetCore" Version="1.1.1" />
<PackageVersion Include="LHZ.WebSocket.AspNetCore" Version="1.1.1" />
<PackageReference Include="LHZ.WebSocket.AspNetCore" />
paket add LHZ.WebSocket.AspNetCore --version 1.1.1
#r "nuget: LHZ.WebSocket.AspNetCore, 1.1.1"
#:package LHZ.WebSocket.AspNetCore@1.1.1
#addin nuget:?package=LHZ.WebSocket.AspNetCore&version=1.1.1
#tool nuget:?package=LHZ.WebSocket.AspNetCore&version=1.1.1
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
UseWebSocketmiddleware for handling WebSocket upgrade requestsIHttpContextwrapper that performs the RFC6455 handshake (withSec-WebSocket-Key/Sec-WebSocket-Versionvalidation) and returns aWebSocketClient- 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.WebSocketpackage (version1.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
Upgradeheader (matched case-insensitively per RFC 7230). - Call
context.HttpUpgrade()orawait context.HttpUpgradeAsync()to perform the handshake and create aWebSocketClient. Invalid handshake requests (missingSec-WebSocket-Keyor a version other than 13) are rejected with a400 Bad Requestresponse. - Use
app.GetWebSocketClientCount()andapp.GetWebSocketClients()to inspect active clients.
API Summary
UseWebSocket(WebSocketUpgradeDelegate webSocketUpgradeDelegate, int timeOut = 10)— Adds middleware to handle upgrades;timeOutlimits seconds to wait for an upgrade.UseWebSocket(Func<IHttpContext, Task> webSocketUpgradeDelegate, int timeOut = 10)— Async overload of the same middleware.GetWebSocketClients()— Returns activeWebSocketClientinstances for theIApplicationBuilder.GetWebSocketClientCount()— Returns the number of active clients for theIApplicationBuilder.
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 | Versions 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. |
-
net10.0
- LHZ.WebSocket (>= 1.1.1)
-
net5.0
- LHZ.WebSocket (>= 1.1.1)
-
net6.0
- LHZ.WebSocket (>= 1.1.1)
-
net8.0
- LHZ.WebSocket (>= 1.1.1)
-
net9.0
- LHZ.WebSocket (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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