Skip to content

Commit 93e66ac

Browse files
committed
fix: 修复连接数据记录器
1 parent 55b8850 commit 93e66ac

11 files changed

Lines changed: 320 additions & 111 deletions

File tree

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/ClusterManager.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ public async Task StopAsync()
155155
/// </summary>
156156
/// <param name="connectionId">Connection ID / 连接 ID</param>
157157
/// <param name="endpoint">Endpoint path / 端点路径</param>
158-
public async Task RegisterConnectionAsync(string connectionId, string endpoint = null)
158+
/// <param name="remoteIpAddress">Remote IP address / 远程 IP 地址</param>
159+
/// <param name="remotePort">Remote port / 远程端口</param>
160+
public async Task RegisterConnectionAsync(string connectionId, string endpoint = null, string remoteIpAddress = null, int remotePort = 0)
159161
{
160-
await _router.RegisterConnectionAsync(connectionId, endpoint);
162+
await _router.RegisterConnectionAsync(connectionId, endpoint, remoteIpAddress, remotePort);
161163
}
162164

163165
/// <summary>
@@ -241,6 +243,26 @@ public string GetOptimalNode()
241243
return _router.GetOptimalNode();
242244
}
243245

246+
/// <summary>
247+
/// Get connection metadata / 获取连接元数据
248+
/// </summary>
249+
/// <param name="connectionId">Connection ID / 连接ID</param>
250+
/// <returns>Connection metadata or null / 连接元数据或null</returns>
251+
public ConnectionMetadata GetConnectionMetadata(string connectionId)
252+
{
253+
return _router.GetConnectionMetadata(connectionId);
254+
}
255+
256+
/// <summary>
257+
/// Get connection endpoint / 获取连接端点
258+
/// </summary>
259+
/// <param name="connectionId">Connection ID / 连接ID</param>
260+
/// <returns>Endpoint path or null / 端点路径或null</returns>
261+
public string GetConnectionEndpoint(string connectionId)
262+
{
263+
return _router.GetConnectionEndpoint(connectionId);
264+
}
265+
244266
/// <summary>
245267
/// Register cluster nodes from configuration / 从配置注册集群节点
246268
/// </summary>

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/ClusterMessage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ public class WebSocketConnectionRegistration
307307
/// </summary>
308308
public string Endpoint { get; set; }
309309

310+
/// <summary>
311+
/// Remote IP address / 远程 IP 地址
312+
/// </summary>
313+
public string RemoteIpAddress { get; set; }
314+
315+
/// <summary>
316+
/// Remote port / 远程端口
317+
/// </summary>
318+
public int RemotePort { get; set; }
319+
310320
/// <summary>
311321
/// Registration time / 注册时间
312322
/// </summary>

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/ClusterRouter.cs

Lines changed: 128 additions & 70 deletions
Large diffs are not rendered by default.

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/GlobalClusterCenter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ public static class GlobalClusterCenter
2424
/// WebSocket connection provider instance / WebSocket 连接提供者实例
2525
/// </summary>
2626
public static IWebSocketConnectionProvider ConnectionProvider { get; set; }
27+
28+
/// <summary>
29+
/// WebSocket statistics recorder instance / WebSocket 统计记录器实例
30+
/// </summary>
31+
public static Infrastructure.Metrics.IWebSocketStatisticsRecorder StatisticsRecorder { get; set; }
2732
}
2833
}

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Handlers/MvcHandler/MvcChannelHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ public async Task ConnectionEntry(HttpContext context, ILogger<WebSocketRouteMid
213213
{
214214
try
215215
{
216-
await clusterManager.RegisterConnectionAsync(context.Connection.Id, context.Request.Path);
216+
var remoteIpAddress = context.Connection.RemoteIpAddress?.ToString();
217+
var remotePort = context.Connection.RemotePort;
218+
await clusterManager.RegisterConnectionAsync(
219+
context.Connection.Id,
220+
context.Request.Path,
221+
remoteIpAddress,
222+
remotePort);
217223
logger.LogDebug($"Registered connection {context.Connection.Id} with cluster manager");
218224
}
219225
catch (Exception ex)
@@ -363,6 +369,9 @@ await bandwidthLimitManager.WaitForBandwidthAsync(
363369
// 记录消息接收指标
364370
var currentNodeId = Infrastructure.Cluster.GlobalClusterCenter.ClusterContext?.NodeId;
365371
_metricsCollector?.RecordMessageReceived(result.Count, currentNodeId, context.Request.Path);
372+
373+
// 记录统计信息(如果统计记录器可用)
374+
Infrastructure.Cluster.GlobalClusterCenter.StatisticsRecorder?.RecordBytesReceived(context.Connection.Id, result.Count);
366375

367376
// 执行ReceivingData管道
368377
_ = await InvokePipeline(RequestPipelineStage.ReceivingData, context, webSocket, result, buffer);
@@ -561,6 +570,9 @@ private async Task MvcForwardSendData(WebSocket webSocket, HttpContext context,
561570
// 记录消息发送指标
562571
var currentNodeId = Infrastructure.Cluster.GlobalClusterCenter.ClusterContext?.NodeId;
563572
_metricsCollector?.RecordMessageSent(responseBytes.Length, currentNodeId, context.Request.Path);
573+
574+
// 记录统计信息(如果统计记录器可用)
575+
Infrastructure.Cluster.GlobalClusterCenter.StatisticsRecorder?.RecordBytesSent(context.Connection.Id, responseBytes.Length);
564576
}
565577
catch (JsonException ex)
566578
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Cyaim.WebSocketServer.Infrastructure.Metrics
2+
{
3+
/// <summary>
4+
/// Interface for recording WebSocket connection statistics
5+
/// 用于记录 WebSocket 连接统计信息的接口
6+
/// </summary>
7+
public interface IWebSocketStatisticsRecorder
8+
{
9+
/// <summary>
10+
/// Record bytes sent / 记录发送的字节数
11+
/// </summary>
12+
/// <param name="connectionId">Connection ID / 连接 ID</param>
13+
/// <param name="bytes">Bytes sent / 发送的字节数</param>
14+
void RecordBytesSent(string connectionId, int bytes);
15+
16+
/// <summary>
17+
/// Record bytes received / 记录接收的字节数
18+
/// </summary>
19+
/// <param name="connectionId">Connection ID / 连接 ID</param>
20+
/// <param name="bytes">Bytes received / 接收的字节数</param>
21+
void RecordBytesReceived(string connectionId, int bytes);
22+
}
23+
}
24+

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Controllers/ClientController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
6666
// Get connection info / 获取连接信息
6767
ClientConnectionInfo clientInfo = null;
6868

69+
// Get connection metadata from cluster manager / 从集群管理器获取连接元数据
70+
ConnectionMetadata metadata = null;
71+
string endpoint = null;
72+
if (clusterManager != null)
73+
{
74+
metadata = clusterManager.GetConnectionMetadata(connectionId);
75+
endpoint = clusterManager.GetConnectionEndpoint(connectionId);
76+
}
77+
6978
// If connection is on current node, get detailed info / 如果连接在当前节点,获取详细信息
7079
if (targetNodeId == currentNodeId && MvcChannelHandler.Clients != null)
7180
{
@@ -76,7 +85,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
7685
{
7786
ConnectionId = connectionId,
7887
NodeId = targetNodeId,
88+
RemoteIpAddress = metadata?.RemoteIpAddress,
89+
RemotePort = metadata?.RemotePort ?? 0,
7990
State = webSocket.State.ToString(),
91+
ConnectedAt = metadata?.ConnectedAt,
92+
Endpoint = endpoint,
8093
BytesSent = stats?.BytesSent ?? 0,
8194
BytesReceived = stats?.BytesReceived ?? 0,
8295
MessagesSent = stats?.MessagesSent ?? 0,
@@ -96,7 +109,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetAll([FromQuery]
96109
{
97110
ConnectionId = connectionId,
98111
NodeId = targetNodeId,
112+
RemoteIpAddress = metadata?.RemoteIpAddress,
113+
RemotePort = metadata?.RemotePort ?? 0,
99114
State = state,
115+
ConnectedAt = metadata?.ConnectedAt,
116+
Endpoint = endpoint,
100117
BytesSent = 0,
101118
BytesReceived = 0,
102119
MessagesSent = 0,

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Controllers/StatisticsController.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetConnections()
8686
// Get connection info / 获取连接信息
8787
ClientConnectionInfo clientInfo = null;
8888

89+
// Get connection metadata from cluster manager / 从集群管理器获取连接元数据
90+
ConnectionMetadata metadata = null;
91+
string endpoint = null;
92+
if (clusterManager != null)
93+
{
94+
metadata = clusterManager.GetConnectionMetadata(connectionId);
95+
endpoint = clusterManager.GetConnectionEndpoint(connectionId);
96+
}
97+
8998
// If connection is on current node, get detailed info / 如果连接在当前节点,获取详细信息
9099
if (targetNodeId == currentNodeId && Infrastructure.Handlers.MvcHandler.MvcChannelHandler.Clients != null)
91100
{
@@ -96,7 +105,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetConnections()
96105
{
97106
ConnectionId = connectionId,
98107
NodeId = targetNodeId,
108+
RemoteIpAddress = metadata?.RemoteIpAddress,
109+
RemotePort = metadata?.RemotePort ?? 0,
99110
State = webSocket.State.ToString(),
111+
ConnectedAt = metadata?.ConnectedAt,
112+
Endpoint = endpoint,
100113
BytesSent = stats?.BytesSent ?? 0,
101114
BytesReceived = stats?.BytesReceived ?? 0,
102115
MessagesSent = stats?.MessagesSent ?? 0,
@@ -116,7 +129,11 @@ public ActionResult<ApiResponse<List<ClientConnectionInfo>>> GetConnections()
116129
{
117130
ConnectionId = connectionId,
118131
NodeId = targetNodeId,
132+
RemoteIpAddress = metadata?.RemoteIpAddress,
133+
RemotePort = metadata?.RemotePort ?? 0,
119134
State = state,
135+
ConnectedAt = metadata?.ConnectedAt,
136+
Endpoint = endpoint,
120137
BytesSent = 0,
121138
BytesReceived = 0,
122139
MessagesSent = 0,

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Middlewares/DashboardMiddlewareExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.Extensions.DependencyInjection;
3+
using Cyaim.WebSocketServer.Infrastructure.Cluster;
34

45
namespace Cyaim.WebSocketServer.Dashboard.Middlewares
56
{
@@ -31,6 +32,13 @@ public static IApplicationBuilder UseWebSocketDashboard(
3132
this IApplicationBuilder app,
3233
string dashboardPath = "/dashboard")
3334
{
35+
// Register statistics recorder to GlobalClusterCenter / 将统计记录器注册到 GlobalClusterCenter
36+
var statisticsService = app.ApplicationServices.GetService<Services.DashboardStatisticsService>();
37+
if (statisticsService != null)
38+
{
39+
GlobalClusterCenter.StatisticsRecorder = statisticsService;
40+
}
41+
3442
app.UseMiddleware<DashboardMiddleware>(dashboardPath);
3543
return app;
3644
}

Cyaim.WebSocketServer/Dashboard/Cyaim.WebSocketServer.Dashboard/Models/DashboardModels.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,22 @@ public class ClientConnectionInfo
112112
/// <summary>
113113
/// Bytes sent / 发送的字节数
114114
/// </summary>
115-
public long BytesSent { get; set; }
115+
public ulong BytesSent { get; set; }
116116

117117
/// <summary>
118118
/// Bytes received / 接收的字节数
119119
/// </summary>
120-
public long BytesReceived { get; set; }
120+
public ulong BytesReceived { get; set; }
121121

122122
/// <summary>
123123
/// Messages sent / 发送的消息数
124124
/// </summary>
125-
public long MessagesSent { get; set; }
125+
public ulong MessagesSent { get; set; }
126126

127127
/// <summary>
128128
/// Messages received / 接收的消息数
129129
/// </summary>
130-
public long MessagesReceived { get; set; }
130+
public ulong MessagesReceived { get; set; }
131131
}
132132

133133
/// <summary>
@@ -138,12 +138,12 @@ public class BandwidthStatistics
138138
/// <summary>
139139
/// Total bytes sent / 总发送字节数
140140
/// </summary>
141-
public long TotalBytesSent { get; set; }
141+
public ulong TotalBytesSent { get; set; }
142142

143143
/// <summary>
144144
/// Total bytes received / 总接收字节数
145145
/// </summary>
146-
public long TotalBytesReceived { get; set; }
146+
public ulong TotalBytesReceived { get; set; }
147147

148148
/// <summary>
149149
/// Bytes sent per second / 每秒发送字节数
@@ -158,12 +158,12 @@ public class BandwidthStatistics
158158
/// <summary>
159159
/// Total messages sent / 总发送消息数
160160
/// </summary>
161-
public long TotalMessagesSent { get; set; }
161+
public ulong TotalMessagesSent { get; set; }
162162

163163
/// <summary>
164164
/// Total messages received / 总接收消息数
165165
/// </summary>
166-
public long TotalMessagesReceived { get; set; }
166+
public ulong TotalMessagesReceived { get; set; }
167167

168168
/// <summary>
169169
/// Messages sent per second / 每秒发送消息数

0 commit comments

Comments
 (0)