11package com .back .global .websocket .controller ;
22
33import com .back .global .exception .CustomException ;
4- import java .security .Principal ;
54import com .back .global .security .user .CustomUserDetails ;
65import com .back .global .websocket .service .WebSocketSessionManager ;
76import com .back .global .websocket .util .WebSocketErrorHelper ;
87import lombok .RequiredArgsConstructor ;
98import lombok .extern .slf4j .Slf4j ;
9+ import org .springframework .messaging .handler .annotation .MessageExceptionHandler ;
1010import org .springframework .messaging .handler .annotation .MessageMapping ;
11- import org .springframework .messaging .handler .annotation .Payload ;
1211import org .springframework .messaging .simp .SimpMessageHeaderAccessor ;
1312import org .springframework .security .core .Authentication ;
1413import org .springframework .stereotype .Controller ;
1514
15+ import java .security .Principal ;
16+
1617@ Slf4j
1718@ Controller
1819@ RequiredArgsConstructor
@@ -23,48 +24,41 @@ public class WebSocketMessageController {
2324
2425 // Heartbeat 처리
2526 @ MessageMapping ("/heartbeat" )
26- public void handleHeartbeat (Principal principal ,
27- SimpMessageHeaderAccessor headerAccessor ) {
28- try {
29- // Principal에서 인증된 사용자 정보 추출
30- if (principal instanceof Authentication auth && auth .getPrincipal () instanceof CustomUserDetails userDetails ) {
31- Long userId = userDetails .getUserId ();
32-
33- sessionManager .updateLastActivity (userId );
34- log .debug ("Heartbeat 처리 완료 - 사용자: {}" , userId );
35- } else {
36- log .warn ("인증되지 않은 Heartbeat 요청: {}" , headerAccessor .getSessionId ());
37- errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
38- }
39- } catch (CustomException e ) {
40- log .error ("Heartbeat 처리 실패: {}" , e .getMessage ());
41- errorHelper .sendCustomExceptionToUser (headerAccessor .getSessionId (), e );
42- } catch (Exception e ) {
43- log .error ("Heartbeat 처리 중 예상치 못한 오류" , e );
44- errorHelper .sendGenericErrorToUser (headerAccessor .getSessionId (), e , "Heartbeat 처리 중 오류가 발생했습니다" );
27+ public void handleHeartbeat (Principal principal , SimpMessageHeaderAccessor headerAccessor ) {
28+ if (principal instanceof Authentication auth && auth .getPrincipal () instanceof CustomUserDetails userDetails ) {
29+ Long userId = userDetails .getUserId ();
30+ sessionManager .updateLastActivity (userId );
31+ log .debug ("Heartbeat 처리 완료 - 사용자: {}" , userId );
32+ } else {
33+ log .warn ("인증되지 않은 Heartbeat 요청: {}" , headerAccessor .getSessionId ());
34+ errorHelper .sendUnauthorizedError (headerAccessor .getSessionId ());
4535 }
4636 }
4737
4838 // 사용자 활동 신호 처리
4939 @ MessageMapping ("/activity" )
50- public void handleActivity (Principal principal ,
51- SimpMessageHeaderAccessor headerAccessor ) {
52- try {
53- if (principal instanceof Authentication auth && auth .getPrincipal () instanceof CustomUserDetails userDetails ) {
54- Long userId = userDetails .getUserId ();
55-
56- sessionManager .updateLastActivity (userId );
57- log .debug ("사용자 활동 신호 처리 완료 - 사용자: {}" , userId );
58- } else {
59- log .warn ("유효하지 않은 활동 신호: userId가 null" );
60- errorHelper .sendInvalidRequestError (headerAccessor .getSessionId (), "사용자 ID가 필요합니다" );
61- }
62- } catch (CustomException e ) {
63- log .error ("활동 신호 처리 실패: {}" , e .getMessage ());
64- errorHelper .sendCustomExceptionToUser (headerAccessor .getSessionId (), e );
65- } catch (Exception e ) {
66- log .error ("활동 신호 처리 중 예상치 못한 오류" , e );
67- errorHelper .sendGenericErrorToUser (headerAccessor .getSessionId (), e , "활동 신호 처리 중 오류가 발생했습니다" );
40+ public void handleActivity (Principal principal , SimpMessageHeaderAccessor headerAccessor ) {
41+ if (principal instanceof Authentication auth && auth .getPrincipal () instanceof CustomUserDetails userDetails ) {
42+ Long userId = userDetails .getUserId ();
43+ sessionManager .updateLastActivity (userId );
44+ log .debug ("사용자 활동 신호 처리 완료 - 사용자: {}" , userId );
45+ } else {
46+ log .warn ("유효하지 않은 활동 신호: 인증 정보 없음" );
47+ errorHelper .sendInvalidRequestError (headerAccessor .getSessionId (), "사용자 ID가 필요합니다" );
6848 }
6949 }
50+
51+ // WebSocket 메시지 처리 중 발생하는 CustomException 처리
52+ @ MessageExceptionHandler (CustomException .class )
53+ public void handleCustomException (CustomException e , SimpMessageHeaderAccessor headerAccessor ) {
54+ log .error ("WebSocket 처리 중 CustomException 발생: {}" , e .getMessage ());
55+ errorHelper .sendCustomExceptionToUser (headerAccessor .getSessionId (), e );
56+ }
57+
58+ // 예상치 못한 모든 Exception 처리
59+ @ MessageExceptionHandler (Exception .class )
60+ public void handleGeneralException (Exception e , SimpMessageHeaderAccessor headerAccessor ) {
61+ log .error ("WebSocket 처리 중 예상치 못한 오류 발생" , e );
62+ errorHelper .sendGenericErrorToUser (headerAccessor .getSessionId (), e , "요청 처리 중 서버 오류가 발생했습니다." );
63+ }
7064}
0 commit comments