@@ -11,6 +11,7 @@ public static class ApiMiddlewareExtensions
1111 /// </summary>
1212 public static IApplicationBuilder UseApiMiddleware ( this IApplicationBuilder app , IWebHostEnvironment environment )
1313 {
14+ var configuration = app . ApplicationServices . GetRequiredService < IConfiguration > ( ) ;
1415 // 개발 환경 설정
1516 if ( environment . IsDevelopment ( ) ) {
1617 app . UseSwagger ( ) ;
@@ -23,8 +24,9 @@ public static IApplicationBuilder UseApiMiddleware(this IApplicationBuilder app,
2324 // 전역 예외 처리
2425 app . UseGlobalExceptionHandler ( ) ;
2526
26- // WebSocket 지원
27- app . UseWebSockets ( ) ;
27+ // WebSocket 지원 - 구성 가능한 옵션 사용
28+ var webSocketOptions = GetWebSocketOptions ( configuration ) ;
29+ app . UseWebSockets ( webSocketOptions ) ;
2830
2931 // WebSocket 미들웨어 등록
3032 app . UseMiddleware < WebSocketMiddleware > ( ) ;
@@ -63,5 +65,56 @@ public static IApplicationBuilder UseDevelopmentFeatures(this IApplicationBuilde
6365
6466 return app ;
6567 }
68+
69+ /// <summary>
70+ /// WebSocket 옵션을 구성 파일과 환경 변수에서 가져옵니다
71+ /// </summary>
72+ private static WebSocketOptions GetWebSocketOptions ( IConfiguration configuration )
73+ {
74+ var options = new WebSocketOptions ( ) ;
75+
76+ // KeepAliveInterval 설정 (환경 변수 > appsettings.json 순서)
77+ var keepAliveMinutes = Environment . GetEnvironmentVariable ( "WEBSOCKET_KEEPALIVE_MINUTES" ) ;
78+ if ( string . IsNullOrEmpty ( keepAliveMinutes ) )
79+ {
80+ keepAliveMinutes = configuration . GetValue < string > ( "WebSocket:KeepAliveIntervalMinutes" ) ;
81+ }
82+
83+ if ( double . TryParse ( keepAliveMinutes , out var minutes ) )
84+ {
85+ if ( minutes <= 0 )
86+ {
87+ options . KeepAliveInterval = TimeSpan . Zero ; // KeepAlive 비활성화
88+ }
89+ else
90+ {
91+ options . KeepAliveInterval = TimeSpan . FromMinutes ( minutes ) ;
92+ }
93+ }
94+ else
95+ {
96+ // 기본값: KeepAlive 비활성화 (연결 안정성을 위해)
97+ options . KeepAliveInterval = TimeSpan . Zero ;
98+ }
99+
100+ // 수신 버퍼 크기 설정
101+ var receiveBufferSize = Environment . GetEnvironmentVariable ( "WEBSOCKET_RECEIVE_BUFFER_SIZE" ) ??
102+ configuration . GetValue < string > ( "WebSocket:ReceiveBufferSize" ) ;
103+ if ( int . TryParse ( receiveBufferSize , out var recvSize ) && recvSize > 0 )
104+ {
105+ options . ReceiveBufferSize = recvSize ;
106+ }
107+
108+ // 송신 버퍼 크기 설정 (WebSocketOptions에는 없으므로 로깅만)
109+ var sendBufferSize = Environment . GetEnvironmentVariable ( "WEBSOCKET_SEND_BUFFER_SIZE" ) ??
110+ configuration . GetValue < string > ( "WebSocket:SendBufferSize" ) ;
111+
112+ // 콘솔 로깅으로 설정 확인
113+ Console . WriteLine ( $ "[WebSocket 설정] KeepAlive: { ( options . KeepAliveInterval == TimeSpan . Zero ? "비활성화" : $ "{ options . KeepAliveInterval . TotalMinutes } 분") } , " +
114+ $ "ReceiveBuffer: { options . ReceiveBufferSize } bytes" +
115+ $ "{ ( int . TryParse ( sendBufferSize , out var sendSize ) && sendSize > 0 ? $ ", SendBuffer: { sendSize } bytes (참고용)" : "" ) } ") ;
116+
117+ return options ;
118+ }
66119 }
67120}
0 commit comments