@@ -335,11 +335,36 @@ private async Task MvcForward(HttpContext context, WebSocket webSocket, WebSocke
335335 _ = await InvokePipeline ( RequestPipelineStage . BeforeReceivingData , PipelineContext . CreateReceive ( context , webSocket , null , null , webSocketOption ) ) ;
336336
337337 #region 接收数据
338+ // 接收数据的缓冲区
338339 byte [ ] buffer = ArrayPool < byte > . Shared . Rent ( ReceiveTextBufferSize ) ;
339- while ( ( result = await webSocket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) , CancellationToken . None ) ) . Count > 0 )
340+ bool messageComplete = false ;
341+
342+ try
340343 {
341- try
344+ while ( ! messageComplete )
342345 {
346+ // 接收数据帧
347+ result = await webSocket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) , CancellationToken . None ) ;
348+
349+ // 如果接收到Close消息,直接退出
350+ if ( result . MessageType == WebSocketMessageType . Close )
351+ {
352+ break ;
353+ }
354+
355+ // 如果Count为0,检查是否消息已完成
356+ // 正常情况下,Count应该大于0,但如果EndOfMessage为true,说明消息接收完成
357+ if ( result . Count == 0 )
358+ {
359+ if ( result . EndOfMessage )
360+ {
361+ messageComplete = true ;
362+ break ;
363+ }
364+ // Count为0但EndOfMessage为false的情况不应该发生,但为了安全继续等待
365+ continue ;
366+ }
367+
343368 // 请求大小限制
344369 if ( wsReceiveReader . Length > webSocketOption . MaxRequestReceiveDataLimit )
345370 {
@@ -374,7 +399,7 @@ await bandwidthLimitManager.WaitForBandwidthAsync(
374399 CancellationToken . None ) ;
375400 }
376401
377- //await wsReceiveReader.WriteAsync(buffer, 0, result.Count);
402+ // 将接收到的数据写入MemoryStream
378403 await wsReceiveReader . WriteAsync ( buffer . AsMemory ( 0 , result . Count ) ) ;
379404
380405 // 记录消息接收指标
@@ -387,28 +412,38 @@ await bandwidthLimitManager.WaitForBandwidthAsync(
387412 // 执行ReceivingData管道
388413 _ = await InvokePipeline ( RequestPipelineStage . ReceivingData , PipelineContext . CreateReceive ( context , webSocket , result , buffer , webSocketOption ) ) ;
389414
390- // 已经接受完数据了
415+ // 检查消息是否接收完成
416+ // 只有当EndOfMessage为true时,才认为消息接收完成
391417 if ( result . EndOfMessage || result . CloseStatus . HasValue )
392418 {
419+ messageComplete = true ;
393420 break ;
394421 }
422+
423+ // 如果EndOfMessage为false,说明还有更多帧需要接收,继续循环
395424 }
396- catch ( Exception ex )
397- {
398- logger . LogDebug (
399- string . Format ( I18nText . WS_INTERACTIVE_TEXT_TEMPALTE ,
400- context . Connection . RemoteIpAddress ,
401- context . Connection . RemotePort ,
402- context . Connection . Id ,
403- I18nText . ConnectionEntry_ReceivingClientDataException + Environment . NewLine + ex . Message + Environment . NewLine + ex . StackTrace
404- )
405- ) ;
406- }
407- finally
425+ }
426+ catch ( Exception ex )
427+ {
428+ logger . LogDebug (
429+ string . Format ( I18nText . WS_INTERACTIVE_TEXT_TEMPALTE ,
430+ context . Connection . RemoteIpAddress ,
431+ context . Connection . RemotePort ,
432+ context . Connection . Id ,
433+ I18nText . ConnectionEntry_ReceivingClientDataException + Environment . NewLine + ex . Message + Environment . NewLine + ex . StackTrace
434+ )
435+ ) ;
436+ // 发生异常时,如果已经接收到部分数据且EndOfMessage为true,认为消息接收完成
437+ if ( result != null && result . EndOfMessage )
408438 {
409- ArrayPool < byte > . Shared . Return ( buffer ) ;
439+ messageComplete = true ;
410440 }
411441 }
442+ finally
443+ {
444+ // 归还buffer
445+ ArrayPool < byte > . Shared . Return ( buffer ) ;
446+ }
412447 // 如果接收到的消息是Close时,断开连接
413448 if ( result . MessageType == WebSocketMessageType . Close )
414449 {
0 commit comments