@@ -24,127 +24,76 @@ public class UserSessionService {
2424
2525 // 세션 등록
2626 public void registerSession (Long userId , String sessionId ) {
27- try {
28- // 기존 세션 확인 및 제거
29- WebSocketSessionInfo existingSession = redisSessionStore .getUserSession (userId );
30- if (existingSession != null ) {
31- terminateSession (existingSession .sessionId ());
32- log .info ("기존 세션 제거 후 새 세션 등록 - 사용자: {}" , userId );
33- }
34-
35- // 새 세션 생성 및 등록
36- WebSocketSessionInfo newSession = WebSocketSessionInfo .createNewSession (userId , sessionId );
37- redisSessionStore .saveUserSession (userId , newSession );
38- redisSessionStore .saveSessionUserMapping (sessionId , userId );
39-
40- log .info ("WebSocket 세션 등록 완료 - 사용자: {}, 세션: {}" , userId , sessionId );
41-
42- } catch (Exception e ) {
43- log .error ("WebSocket 세션 등록 실패 - 사용자: {}" , userId , e );
44- throw new CustomException (ErrorCode .WS_CONNECTION_FAILED );
27+ WebSocketSessionInfo existingSession = redisSessionStore .getUserSession (userId );
28+ if (existingSession != null ) {
29+ terminateSession (existingSession .sessionId ());
30+ log .info ("기존 세션 제거 후 새 세션 등록 - 사용자: {}" , userId );
4531 }
32+
33+ WebSocketSessionInfo newSession = WebSocketSessionInfo .createNewSession (userId , sessionId );
34+ redisSessionStore .saveUserSession (userId , newSession );
35+ redisSessionStore .saveSessionUserMapping (sessionId , userId );
36+
37+ log .info ("WebSocket 세션 등록 완료 - 사용자: {}, 세션: {}" , userId , sessionId );
4638 }
4739
4840 // 세션 종료
4941 public void terminateSession (String sessionId ) {
50- try {
51- Long userId = redisSessionStore .getUserIdBySession (sessionId );
52-
53- if (userId != null ) {
54- // 세션 데이터 삭제
55- redisSessionStore .deleteUserSession (userId );
56- redisSessionStore .deleteSessionUserMapping (sessionId );
57-
58- log .info ("WebSocket 세션 종료 완료 - 세션: {}, 사용자: {}" , sessionId , userId );
59- } else {
60- log .warn ("종료할 세션을 찾을 수 없음 - 세션: {}" , sessionId );
61- }
62-
63- } catch (Exception e ) {
64- log .error ("WebSocket 세션 종료 실패 - 세션: {}" , sessionId , e );
65- throw new CustomException (ErrorCode .WS_REDIS_ERROR );
42+ Long userId = redisSessionStore .getUserIdBySession (sessionId );
43+
44+ if (userId != null ) {
45+ redisSessionStore .deleteUserSession (userId );
46+ redisSessionStore .deleteSessionUserMapping (sessionId );
47+ log .info ("WebSocket 세션 종료 완료 - 세션: {}, 사용자: {}" , sessionId , userId );
48+ } else {
49+ log .warn ("종료할 세션을 찾을 수 없음 - 세션: {}" , sessionId );
6650 }
6751 }
6852
6953 // Heartbeat 처리 (활동 시간 업데이트 및 TTL 연장)
7054 public void processHeartbeat (Long userId ) {
71- try {
72- WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
73-
74- if (sessionInfo == null ) {
75- log .warn ("세션 정보가 없어 Heartbeat 처리 실패 - 사용자: {}" , userId );
76- return ;
77- }
78-
79- // 마지막 활동 시간 업데이트
80- WebSocketSessionInfo updatedSession = sessionInfo .withUpdatedActivity ();
55+ WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
8156
82- // TTL 연장하며 저장
83- redisSessionStore .saveUserSession (userId , updatedSession );
57+ if (sessionInfo == null ) {
58+ log .warn ("세션 정보가 없어 Heartbeat 처리 실패 - 사용자: {}" , userId );
59+ return ;
60+ }
8461
85- log .debug ("Heartbeat 처리 완료 - 사용자: {}, TTL 연장" , userId );
62+ WebSocketSessionInfo updatedSession = sessionInfo .withUpdatedActivity ();
63+ redisSessionStore .saveUserSession (userId , updatedSession );
8664
87- } catch (Exception e ) {
88- log .error ("Heartbeat 처리 실패 - 사용자: {}" , userId , e );
89- throw new CustomException (ErrorCode .WS_ACTIVITY_UPDATE_FAILED );
90- }
65+ log .debug ("Heartbeat 처리 완료 - 사용자: {}, TTL 연장" , userId );
9166 }
9267
9368 // 사용자 연결 상태 확인
9469 public boolean isConnected (Long userId ) {
95- try {
96- return redisSessionStore .existsUserSession (userId );
97- } catch (Exception e ) {
98- log .error ("사용자 연결 상태 확인 실패 - 사용자: {}" , userId , e );
99- throw new CustomException (ErrorCode .WS_REDIS_ERROR );
100- }
70+ return redisSessionStore .existsUserSession (userId );
10171 }
10272
10373 // 사용자 세션 정보 조회
10474 public WebSocketSessionInfo getSessionInfo (Long userId ) {
105- try {
106- WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
75+ WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
10776
108- if (sessionInfo == null ) {
109- log .debug ("세션 정보 없음 - 사용자: {}" , userId );
110- }
111-
112- return sessionInfo ;
113-
114- } catch (Exception e ) {
115- log .error ("세션 정보 조회 실패 - 사용자: {}" , userId , e );
116- throw new CustomException (ErrorCode .WS_REDIS_ERROR );
77+ if (sessionInfo == null ) {
78+ log .debug ("세션 정보 없음 - 사용자: {}" , userId );
11779 }
80+
81+ return sessionInfo ;
11882 }
11983
12084 // 세션ID로 사용자ID 조회
12185 public Long getUserIdBySessionId (String sessionId ) {
122- try {
123- return redisSessionStore .getUserIdBySession (sessionId );
124- } catch (Exception e ) {
125- log .error ("세션으로 사용자 조회 실패 - 세션: {}" , sessionId , e );
126- throw new CustomException (ErrorCode .WS_REDIS_ERROR );
127- }
86+ return redisSessionStore .getUserIdBySession (sessionId );
12887 }
12988
13089 // 사용자의 현재 방 ID 조회
13190 public Long getCurrentRoomId (Long userId ) {
132- try {
133- WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
134- return sessionInfo != null ? sessionInfo .currentRoomId () : null ;
135- } catch (Exception e ) {
136- log .error ("사용자 현재 방 조회 실패 - 사용자: {}" , userId , e );
137- return null ;
138- }
91+ WebSocketSessionInfo sessionInfo = redisSessionStore .getUserSession (userId );
92+ return sessionInfo != null ? sessionInfo .currentRoomId () : null ;
13993 }
14094
14195 // 전체 온라인 사용자 수 조회
14296 public long getTotalOnlineUserCount () {
143- try {
144- return redisSessionStore .getTotalOnlineUserCount ();
145- } catch (Exception e ) {
146- log .error ("전체 온라인 사용자 수 조회 실패" , e );
147- return 0 ;
148- }
97+ return redisSessionStore .getTotalOnlineUserCount ();
14998 }
15099}
0 commit comments