66import com .back .global .websocket .dto .WebSocketSessionInfo ;
77import com .back .global .websocket .service .WebSocketSessionManager ;
88import com .back .global .websocket .util .WebSocketAuthHelper ;
9+ import com .back .global .websocket .webrtc .dto .WebRTCErrorResponse ;
910import com .back .global .websocket .webrtc .dto .media .WebRTCMediaStateResponse ;
1011import com .back .global .websocket .webrtc .dto .media .WebRTCMediaToggleRequest ;
1112import com .back .global .websocket .webrtc .dto .signal .*;
1617import org .springframework .messaging .handler .annotation .MessageExceptionHandler ;
1718import org .springframework .messaging .handler .annotation .MessageMapping ;
1819import org .springframework .messaging .handler .annotation .Payload ;
19- import org .springframework .messaging .simp .SimpMessageHeaderAccessor ;
2020import org .springframework .messaging .simp .SimpMessagingTemplate ;
2121import org .springframework .stereotype .Controller ;
2222import org .springframework .validation .annotation .Validated ;
@@ -35,13 +35,10 @@ public class WebRTCSignalingController {
3535
3636 // WebRTC Offer 메시지 처리
3737 @ MessageMapping ("/webrtc/offer" )
38- public void handleOffer (@ Validated @ Payload WebRTCOfferRequest request ,
39- SimpMessageHeaderAccessor headerAccessor ,
40- Principal principal ) {
38+ public void handleOffer (@ Validated @ Payload WebRTCOfferRequest request , Principal principal ) {
4139 CustomUserDetails userDetails = WebSocketAuthHelper .extractUserDetails (principal );
4240 if (userDetails == null ) {
43- errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
44- return ;
41+ throw new CustomException (ErrorCode .UNAUTHORIZED );
4542 }
4643
4744 Long fromUserId = userDetails .getUserId ();
@@ -66,13 +63,10 @@ public void handleOffer(@Validated @Payload WebRTCOfferRequest request,
6663
6764 // WebRTC Answer 메시지 처리
6865 @ MessageMapping ("/webrtc/answer" )
69- public void handleAnswer (@ Validated @ Payload WebRTCAnswerRequest request ,
70- SimpMessageHeaderAccessor headerAccessor ,
71- Principal principal ) {
66+ public void handleAnswer (@ Validated @ Payload WebRTCAnswerRequest request , Principal principal ) {
7267 CustomUserDetails userDetails = WebSocketAuthHelper .extractUserDetails (principal );
7368 if (userDetails == null ) {
74- errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
75- return ;
69+ throw new CustomException (ErrorCode .UNAUTHORIZED );
7670 }
7771
7872 Long fromUserId = userDetails .getUserId ();
@@ -97,13 +91,10 @@ public void handleAnswer(@Validated @Payload WebRTCAnswerRequest request,
9791
9892 // ICE Candidate 메시지 처리
9993 @ MessageMapping ("/webrtc/ice-candidate" )
100- public void handleIceCandidate (@ Validated @ Payload WebRTCIceCandidateRequest request ,
101- SimpMessageHeaderAccessor headerAccessor ,
102- Principal principal ) {
94+ public void handleIceCandidate (@ Validated @ Payload WebRTCIceCandidateRequest request , Principal principal ) {
10395 CustomUserDetails userDetails = WebSocketAuthHelper .extractUserDetails (principal );
10496 if (userDetails == null ) {
105- errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
106- return ;
97+ throw new CustomException (ErrorCode .UNAUTHORIZED );
10798 }
10899
109100 Long fromUserId = userDetails .getUserId ();
@@ -127,13 +118,10 @@ public void handleIceCandidate(@Validated @Payload WebRTCIceCandidateRequest req
127118
128119 // 미디어 상태 토글 처리 (방 전체에 상태 공유)
129120 @ MessageMapping ("/webrtc/media/toggle" )
130- public void handleMediaToggle (@ Validated @ Payload WebRTCMediaToggleRequest request ,
131- SimpMessageHeaderAccessor headerAccessor ,
132- Principal principal ) {
121+ public void handleMediaToggle (@ Validated @ Payload WebRTCMediaToggleRequest request , Principal principal ) {
133122 CustomUserDetails userDetails = WebSocketAuthHelper .extractUserDetails (principal );
134123 if (userDetails == null ) {
135- errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
136- return ;
124+ throw new CustomException (ErrorCode .UNAUTHORIZED );
137125 }
138126
139127 Long userId = userDetails .getUserId ();
@@ -150,15 +138,41 @@ public void handleMediaToggle(@Validated @Payload WebRTCMediaToggleRequest reque
150138
151139 // WebRTC 시그널링 처리 중 발생하는 CustomException 처리
152140 @ MessageExceptionHandler (CustomException .class )
153- public void handleCustomException (CustomException e , SimpMessageHeaderAccessor headerAccessor ) {
154- log .warn ("WebRTC 시그널링 오류 발생: {}" , e .getMessage ());
155- errorHelper .sendCustomExceptionToUser (headerAccessor .getSessionId (), e );
141+ public void handleCustomException (CustomException e , Principal principal ) {
142+ if (principal == null ) {
143+ log .warn ("인증 정보 없는 사용자의 WebRTC 오류: {}" , e .getMessage ());
144+ return ;
145+ }
146+
147+ log .warn ("WebRTC 시그널링 오류 발생 (to {}): {}" , principal .getName (), e .getMessage ());
148+
149+ WebRTCErrorResponse errorResponse = WebRTCErrorResponse .from (e );
150+
151+ messagingTemplate .convertAndSendToUser (
152+ principal .getName (), // 에러를 발생시킨 사람의 username
153+ "/queue/webrtc" ,
154+ errorResponse
155+ );
156156 }
157157
158158 // 예상치 못한 모든 Exception 처리
159159 @ MessageExceptionHandler (Exception .class )
160- public void handleGeneralException (Exception e , SimpMessageHeaderAccessor headerAccessor ) {
161- log .error ("WebRTC 시그널링 처리 중 예상치 못한 오류 발생" , e );
162- errorHelper .sendGenericErrorToUser (headerAccessor .getSessionId (), e , "시그널링 처리 중 오류가 발생했습니다." );
160+ public void handleGeneralException (Exception e , Principal principal ) { // headerAccessor -> Principal
161+ if (principal == null ) {
162+ log .error ("WebRTC 처리 중 인증 정보 없는 사용자의 예외 발생" , e );
163+ return ;
164+ }
165+
166+ log .error ("WebRTC 시그널링 처리 중 예상치 못한 오류 발생 (to {})" , principal .getName (), e );
167+
168+ // CustomException으로 감싸서 일관된 형식의 에러 DTO를 생성
169+ CustomException customException = new CustomException (ErrorCode .WS_INTERNAL_ERROR );
170+ WebRTCErrorResponse errorResponse = WebRTCErrorResponse .from (customException );
171+
172+ messagingTemplate .convertAndSendToUser (
173+ principal .getName (),
174+ "/queue/webrtc" ,
175+ errorResponse
176+ );
163177 }
164178}
0 commit comments