Skip to content

Commit 044b2c6

Browse files
committed
feat: 实现 #12
1 parent 91ddda0 commit 044b2c6

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Configures/WebSocketRouteOption.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public class WebSocketRouteOption
120120
/// </summary>
121121
public BandwidthLimitPolicy BandwidthLimitPolicy { get; set; }
122122

123+
/// <summary>
124+
/// 是否要求请求必须包含Id属性。如果为true,未包含Id的请求将被拒绝响应。
125+
/// 默认为true,因为客户端需要Id来区分响应来源。
126+
/// </summary>
127+
public bool RequireRequestId { get; set; } = true;
128+
123129
#region Event
124130

125131
/// <summary>

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,37 @@ await bandwidthLimitManager.WaitForBandwidthAsync(
489489
requestBody = body.ValueKind == JsonValueKind.Undefined ? null : (JsonNode.Parse(body.GetRawText())?.AsObject());
490490
}
491491

492+
// 检查请求是否包含Id属性
493+
if (webSocketOption.RequireRequestId && (requestScheme == null || string.IsNullOrWhiteSpace(requestScheme.Id)))
494+
{
495+
// 创建错误响应
496+
MvcResponseScheme errorResponse = new MvcResponseScheme()
497+
{
498+
Status = 1,
499+
RequestTime = requestTime,
500+
CompleteTime = DateTime.Now.Ticks,
501+
Target = requestScheme?.Target,
502+
Id = requestScheme?.Id,
503+
Msg = string.Format(I18nText.WS_INTERACTIVE_TEXT_TEMPALTE, context.Connection.RemoteIpAddress, context.Connection.RemotePort, context.Connection.Id, I18nText.MvcForwardSendData_RequestIdRequired)
504+
};
505+
506+
// 发送错误响应
507+
string serialJson = JsonSerializer.Serialize(errorResponse, webSocketOption.DefaultResponseJsonSerializerOptions);
508+
var responseBytes = Encoding.UTF8.GetBytes(serialJson);
509+
await webSocket.SendAsync(new ArraySegment<byte>(responseBytes), result.MessageType, result.EndOfMessage, CancellationToken.None);
510+
511+
logger.LogInformation(string.Format(I18nText.WS_INTERACTIVE_TEXT_TEMPALTE, context.Connection.RemoteIpAddress, context.Connection.RemotePort, context.Connection.Id, I18nText.MvcForwardSendData_RequestIdRequired));
512+
513+
// 记录消息发送指标
514+
var currentNodeId = Infrastructure.Cluster.GlobalClusterCenter.ClusterContext?.NodeId;
515+
_metricsCollector?.RecordMessageSent(responseBytes.Length, currentNodeId, context.Request.Path);
516+
517+
// 记录统计信息(如果统计记录器可用)
518+
Infrastructure.Cluster.GlobalClusterCenter.StatisticsRecorder?.RecordBytesSent(context.Connection.Id, responseBytes.Length);
519+
520+
continue;
521+
}
522+
492523
// 执行管道 BeforeForwardingData
493524
_ = await InvokePipeline(RequestPipelineStage.BeforeForwardingData, PipelineContext.CreateForward(context, webSocket, result, wsReceiveReader.GetBuffer(), requestScheme, requestBody, webSocketOption));
494525

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/I18nText.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public static void UpdateI18nText(string i18nResourcePath)
142142

143143
public static readonly string MvcDistributeAsync_EndPointNotFound = "Endpoint not found: ";
144144

145+
public static readonly string MvcForwardSendData_RequestIdRequired = "Request must contain an Id property. The client cannot distinguish the response source without an Id." + Environment.NewLine;
146+
145147

146148
}
147149
}

0 commit comments

Comments
 (0)