WSHub.Abstractions
0.0.0.40
See the version list below for details.
dotnet add package WSHub.Abstractions --version 0.0.0.40
NuGet\Install-Package WSHub.Abstractions -Version 0.0.0.40
<PackageReference Include="WSHub.Abstractions" Version="0.0.0.40" />
<PackageVersion Include="WSHub.Abstractions" Version="0.0.0.40" />
<PackageReference Include="WSHub.Abstractions" />
paket add WSHub.Abstractions --version 0.0.0.40
#r "nuget: WSHub.Abstractions, 0.0.0.40"
#:package WSHub.Abstractions@0.0.0.40
#addin nuget:?package=WSHub.Abstractions&version=0.0.0.40
#tool nuget:?package=WSHub.Abstractions&version=0.0.0.40
WSHub
基于 .NET 的现代 WebSocket 即时通讯服务框架,参考并改进自 FreeIM。
职责清晰、可扩展、支持多节点部署,适用于游戏、IM、实时协作等场景。
特性
- 发送与投递分离 —
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 | 在线检查 |
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 | Versions 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. |
-
net10.0
-
net8.0
-
net9.0
NuGet packages (3)
Showing the top 3 NuGet packages that depend on WSHub.Abstractions:
| Package | Downloads |
|---|---|
|
WSHub.InMemory
In-memory storage backend for WSHub — suitable for development, testing, and single-pod deployments. Includes InMemoryConnectionRegistry, InMemoryTokenStore, and InMemoryMessageBus. |
|
|
WSHub.AspNetCore
ASP.NET Core integration for WSHub — WebSocket connection handler, message dispatch pipeline (Send/Delivery separation), background services for Redis bus subscription, and minimal API endpoint support. |
|
|
WSHub.FreeRedis
Redis storage backend for WSHub (FreeRedis-based) — suitable for production multi-pod deployments. Includes RedisConnectionRegistry, AggregatedMessageListener (refcount-based channel subscription), RedisMessagePublisher, and RedisTokenStore. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.0.41 | 42 | 8/11/2026 |
| 0.0.0.40 | 41 | 8/11/2026 |
| 0.0.0.39 | 63 | 8/8/2026 |
| 0.0.0.38 | 74 | 8/8/2026 |
| 0.0.0.37 | 75 | 8/8/2026 |
| 0.0.0.36 | 68 | 8/8/2026 |
| 0.0.0.35 | 71 | 8/8/2026 |
| 0.0.0.34 | 69 | 8/7/2026 |
| 0.0.0.33 | 175 | 8/5/2026 |
| 0.0.0.32 | 194 | 7/27/2026 |
| 0.0.0.31 | 148 | 7/27/2026 |
| 0.0.0.30 | 152 | 7/27/2026 |
| 0.0.0.29 | 145 | 7/26/2026 |
| 0.0.0.27 | 148 | 7/26/2026 |
| 0.0.0.26 | 151 | 7/26/2026 |
| 0.0.0.25 | 234 | 7/2/2026 |
| 0.0.0.24 | 160 | 7/1/2026 |
| 0.0.0.22 | 175 | 7/1/2026 |
| 0.0.0.21 | 159 | 7/1/2026 |
| 0.0.0.19 | 156 | 7/1/2026 |