本文档详细介绍 Cyaim.WebSocketServer.Dashboard 的功能和使用方法。
Cyaim.WebSocketServer.Dashboard 是一个用于监控和管理 WebSocketServer 服务端(包含集群)的仪表板应用,提供:
- 📊 实时监控 - 集群状态、连接数、消息统计
- 🖥️ 节点管理 - 查看和管理集群节点
- 👥 客户端管理 - 查看所有客户端连接信息
- 📈 性能分析 - 带宽监控、消息统计
- 🔄 消息测试 - 发送测试消息
- ✅ 集群概览 - 查看所有节点状态、连接数、Raft 状态
- ✅ 节点管理 - 查看和管理集群节点
- ✅ 客户端管理 - 查看所有客户端连接信息、统计信息
- ✅ 消息发送 - 向指定连接发送测试消息
- ✅ 路由管理 - 查看连接路由表
- ✅ 统计信息 - 带宽统计、连接统计
- ✅ 现代化 UI - 基于 Svelte 5 和 Tailwind CSS
- ✅ 实时更新 - 自动刷新数据
- ✅ 响应式设计 - 支持移动端和桌面端
- ✅ 国际化支持 - 支持中文和英文
dotnet add package Cyaim.WebSocketServer.Dashboardusing Cyaim.WebSocketServer.Dashboard.Middlewares;
var builder = WebApplication.CreateBuilder(args);
// 添加 Dashboard 服务
builder.Services.AddWebSocketDashboard();
// 配置 WebSocketServer
builder.Services.ConfigureWebSocketRoute(x =>
{
// WebSocketServer 配置
});var app = builder.Build();
// 使用 Dashboard 中间件(必须在 MapControllers 之后)
app.UseWebSocketDashboard("/dashboard");启动应用后,访问 http://localhost:5000/dashboard 查看 Dashboard。
GET /ws_server/api/dashboard/cluster/overview响应:
{
"success": true,
"data": {
"totalNodes": 3,
"connectedNodes": 3,
"totalConnections": 100,
"localConnections": 30,
"currentNodeId": "node1",
"isCurrentNodeLeader": true,
"nodes": [...]
}
}GET /ws_server/api/dashboard/cluster/nodesGET /ws_server/api/dashboard/cluster/nodes/{nodeId}GET /ws_server/api/client查询参数:
nodeId(可选): 节点 ID 过滤器
GET /ws_server/api/client?nodeId=node1GET /ws_server/api/client/count响应:
{
"success": true,
"data": {
"total": 100,
"local": 30
}
}POST /ws_server/api/messages/send
Content-Type: application/json
{
"connectionId": "connection-id",
"content": "Hello, World!",
"messageType": "Text"
}POST /ws_server/api/messages/broadcast
Content-Type: application/json
{
"content": "Broadcast message",
"messageType": "Text"
}POST /ws_server/api/messages/broadcast/node/{nodeId}
Content-Type: application/json
{
"content": "Node broadcast",
"messageType": "Text"
}GET /ws_server/api/routes响应:
{
"success": true,
"data": {
"connection-id-1": "node1",
"connection-id-2": "node2"
}
}GET /ws_server/api/routes/{connectionId}GET /ws_server/api/statistics/bandwidth响应:
{
"success": true,
"data": {
"totalBytesSent": 1024000,
"totalBytesReceived": 2048000,
"totalMessagesSent": 1000,
"totalMessagesReceived": 2000,
"bytesSentPerSecond": 1024,
"bytesReceivedPerSecond": 2048,
"messagesSentPerSecond": 10,
"messagesReceivedPerSecond": 20
}
}GET /ws_server/api/statistics/connections- 框架: SvelteKit 2.x
- UI 库: Svelte 5
- 样式: Tailwind CSS 4.x
- 国际化: Paraglide.js (inlang)
- 构建工具: Vite 7.x
dashboard/
├── overview/ # 集群概览
├── nodes/ # 节点管理
├── clients/ # 客户端列表
├── bandwidth/ # 带宽监控
├── dataflow/ # 数据流查看
└── send/ # 消息发送
cd Dashboard/websocketserver-dashboard
pnpm install
pnpm devpnpm buildbuilder.Services.AddWebSocketDashboard();这会注册:
DashboardStatisticsService- 统计服务DashboardHelperService- 辅助服务IWebSocketStatisticsRecorder- 统计记录器接口
app.UseWebSocketDashboard("/dashboard");参数:
pathPrefix(可选): Dashboard 路径前缀,默认/dashboard
如果前端和后端分离部署,需要配置 CORS:
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowDashboard", policy =>
{
policy.WithOrigins("http://localhost:5173")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
app.UseCors("AllowDashboard");- 合理设置数据刷新间隔
- 使用分页加载大量数据
- 缓存静态数据
- 在生产环境中限制 Dashboard 访问
- 使用身份验证和授权
- 配置 HTTPS
- 设置关键指标告警
- 监控 Dashboard 性能
- 记录访问日志
- 提供清晰的错误提示
- 优化加载速度
- 支持响应式设计