WSHub.AspNetCore 0.0.0.41

dotnet add package WSHub.AspNetCore --version 0.0.0.41
                    
NuGet\Install-Package WSHub.AspNetCore -Version 0.0.0.41
                    
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="WSHub.AspNetCore" Version="0.0.0.41" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WSHub.AspNetCore" Version="0.0.0.41" />
                    
Directory.Packages.props
<PackageReference Include="WSHub.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 WSHub.AspNetCore --version 0.0.0.41
                    
#r "nuget: WSHub.AspNetCore, 0.0.0.41"
                    
#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 WSHub.AspNetCore@0.0.0.41
                    
#: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=WSHub.AspNetCore&version=0.0.0.41
                    
Install as a Cake Addin
#tool nuget:?package=WSHub.AspNetCore&version=0.0.0.41
                    
Install as a Cake Tool

WSHub

基于 .NET 的现代 WebSocket 即时通讯服务框架,参考并改进自 FreeIM

职责清晰、可扩展、支持多节点部署,适用于游戏、IM、实时协作等场景。

.NET Coverage Tests License

特性

  • 发送与投递分离IMessageHandler(发送) + IMessageDeliveryHandler(投递)独立管道
  • 多节点支持 — 水平扩展,通过 Redis(FreeRedis)或 InMemory 进行消息路由
  • 多租户 — tenantId 贯穿连接/频道/消息全栈
  • 二进制消息 — byte[] 管道 + 路由头(0xFE),支持 protobuf
  • 连接管理 — 心跳、超时清理、per-connection 发送队列
  • 事件机制 — 上线/下线/断连/频道进出事件
  • 多目标框架net8.0 (LTS) / net9.0 / net10.0
  • Native AOT — InMemory 模式支持 Docker AOT 部署
  • 高覆盖率 — 551 单元测试 + 集成测试(详见 CI)

快速开始

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddWSHub<long>(options =>
{
    options.KeyPrefix = "wshub_v1";
    options.Servers = new[] { "localhost:5299" };
    options.Server = "localhost:5299";
})
.AddClientIdConverter<long, LongClientIdConverter>()
.UseInMemoryStorage<long>()       // 开发环境;生产用 .UseFreeRedisStorage(redis)
.AddDefaultHandlers<long>();      // 一键注册 Handler + Dispatcher + 后台服务

var app = builder.Build();
app.UseWebSockets();

app.Map("/ws/{tenantId?}", async (string? tenantId, HttpContext context) =>
{
    var handler = context.RequestServices.GetRequiredService<WebSocketConnectionHandler<long>>();
    await handler.HandleAsync(tenantId, context);
});

app.Run();

完整示例见 samples/MinimalApiSample/

架构概览

客户端 WebSocket ──→ ProcessClientMessage ──→ MessageHandlerDispatcher ──→ IMessageHandler (发送)
                                                                                │
应用层主动发送 ──→ IApplicationMessageService ──→ MessageHandlerDispatcher ──→ IMessageHandler (发送)
                                                                                │
                                                                        IMessagePublisher
                                                                                │
                                                                    Redis / InMemory 消息总线
                                                                                │
                                                                        IMessageListener
                                                                                │
                                                                        HandleIncomingMessage
                                                                                │
                                                                        MessageDeliveryDispatcher ──→ IMessageDeliveryHandler (投递)
                                                                                                        │
                                                                                                ConnectionManager ──→ WebSocket

项目结构

WSHub/
├── src/
│   ├── WSHub.Abstractions/       # 接口、模型、Options
│   ├── WSHub.AspNetCore/         # ASP.NET Core 集成
│   ├── WSHub.Host/              # Control HTTP API 独立宿主
│   ├── WSHub.InMemory/           # InMemory 存储后端(开发/测试)
│   └── WSHub.FreeRedis/          # FreeRedis 存储后端(生产/多节点)
├── samples/
│   ├── MinimalApiSample/         # 基础示例 + 测试页面
│   ├── ImDemo/                   # IM 频道演示(多租户 + Channel Join)
│   ├── GameDemo/                 # Protobuf HTML5 实时游戏
│   └── AotDemo/                  # Native AOT Docker 部署示例
├── benchmarks/
│   └── WSHub.Benchmarks/         # BenchmarkDotNet 性能基准
├── tests/                        # 6 个测试项目,551 测试
├── docs/                         # 架构 + 协议文档 + 性能分析
└── .github/workflows/            # CI/CD (coverage + publish + DingTalk)

性能 (BenchmarkDotNet)

操作 延迟 说明
PublishAsync 140 ns 单条消息发布
PublishBatchAsync ×100 13.9 μs 批量发布
RegisterAsync 1.08 μs / 248 B 连接注册
HasOnlineAsync 2.3 ns 在线检查

详见 docs/WSHub性能优化总结-1至10.md

Samples

项目 说明
MinimalApiSample 基础 WebSocket + Channel + Broadcast
ImDemo IM 频道演示(多租户 + Channel Join/Leave)
GameDemo Protobuf HTML5 多人实时游戏(手写 JS protobuf 编解码器)
AotDemo Native AOT + Docker 部署(docker compose up -d

运行测试

dotnet test tests/WSHub.Abstractions.Tests/
dotnet test tests/WSHub.AspNetCore.Tests/
dotnet test tests/WSHub.InMemory.Tests/
dotnet test tests/WSHub.FreeRedis.Tests/
dotnet test tests/WSHub.IntegrationTests/
dotnet test tests/WSHub.AotTests/

运行 Benchmark

dotnet run -c Release --project benchmarks/WSHub.Benchmarks/

CI/CD

Workflow 触发 说明
coverage.yml push / PR 覆盖率报告 (dotnet-coverage)
publish-nuget.yml release / manual NuGet 发布 + 钉钉通知

贡献

欢迎提交 Issue 和 Pull Request。

License

MIT

Product Compatible and additional computed target framework versions.
.NET 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
0.0.0.41 82 8/11/2026
0.0.0.40 70 8/11/2026
0.0.0.39 86 8/8/2026
0.0.0.38 99 8/8/2026
0.0.0.37 88 8/8/2026
0.0.0.36 86 8/8/2026
0.0.0.35 88 8/8/2026
0.0.0.34 89 8/7/2026
0.0.0.33 124 8/5/2026
0.0.0.32 144 7/27/2026
0.0.0.31 87 7/27/2026
0.0.0.30 92 7/27/2026
0.0.0.29 93 7/26/2026
0.0.0.27 100 7/26/2026
0.0.0.26 101 7/26/2026
0.0.0.25 192 7/2/2026
0.0.0.24 94 7/1/2026
0.0.0.22 120 7/1/2026
0.0.0.21 103 7/1/2026
0.0.0.19 101 7/1/2026
Loading failed