LHZ.WebSocket.AspNetCore
1.3.0
dotnet add package LHZ.WebSocket.AspNetCore --version 1.3.0
NuGet\Install-Package LHZ.WebSocket.AspNetCore -Version 1.3.0
<PackageReference Include="LHZ.WebSocket.AspNetCore" Version="1.3.0" />
<PackageVersion Include="LHZ.WebSocket.AspNetCore" Version="1.3.0" />
<PackageReference Include="LHZ.WebSocket.AspNetCore" />
paket add LHZ.WebSocket.AspNetCore --version 1.3.0
#r "nuget: LHZ.WebSocket.AspNetCore, 1.3.0"
#:package LHZ.WebSocket.AspNetCore@1.3.0
#addin nuget:?package=LHZ.WebSocket.AspNetCore&version=1.3.0
#tool nuget:?package=LHZ.WebSocket.AspNetCore&version=1.3.0
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 (validatingConnection,Sec-WebSocket-KeyandSec-WebSocket-Version) and returns aWebSocketClient- Asynchronous upgrade API
HttpUpgradeAsync(the synchronousHttpUpgradeis obsolete) - Thread-safe registration and removal of connected clients, shared across branched pipelines
- Optional upgrade timeout control
Requirements
- .NET 5 / 6 / 8 / 9 / 10
LHZ.WebSocketpackage (version1.3.0)
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.OnCloseReceived += (c, reason) => c.Close();
});
app.Run();
Notes:
- The delegate is invoked only when the request carries a WebSocket
Upgradeheader. The token is matched case-insensitively and is also recognised inside a comma-separated field such asUpgrade: h2c, websocket(RFC 7230). - Call
await context.HttpUpgradeAsync()to perform the handshake and create aWebSocketClient. Invalid handshake requests are rejected with a400 Bad Requestresponse: aConnectionheader without theUpgradetoken, a missingSec-WebSocket-Keyor one that is not a base64-encoded 16-byte value, or aSec-WebSocket-Versionother than 13. - A context can only be upgraded once; a second attempt throws
InvalidOperationException. - If the upgrade itself fails (for example the client disconnects mid-handshake), the context ends up
Rejectedand the exception is rethrown. Disposing a context while its handshake is still in progress has the same effect: the pending upgrade throwsInvalidOperationExceptionand its stream is released. - 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 the activeWebSocketClientinstances for the application. Clients are tracked per application, so those registered from a pipeline branch built byMaporUseWhenare also returned here.GetWebSocketClientCount()— Returns the number of active clients for the application.
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.3.0)
-
net5.0
- LHZ.WebSocket (>= 1.3.0)
-
net6.0
- LHZ.WebSocket (>= 1.3.0)
-
net8.0
- LHZ.WebSocket (>= 1.3.0)
-
net9.0
- LHZ.WebSocket (>= 1.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.3.0:Fix a context stuck in the Upgrading state when the connection upgrade fails or the context is disposed mid-handshake (it now becomes Rejected and the upgraded stream is released); share one header-token parser between the Upgrade and Connection checks; run the handshake timeout watcher without an extra Task.Run