Skip to content

Commit 5b3a429

Browse files
committed
hotfix:로젝 내 웹소켓 세션 제거
1 parent 4cb7de7 commit 5b3a429

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/main/java/com/back/global/websocket/service/RoomParticipantService.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,27 @@ public void handleSessionDisconnected(SessionDisconnectedEvent event) {
4545

4646
// 사용자 방 입장 (아바타 정보 포함)
4747
public void enterRoom(Long userId, Long roomId, Long avatarId) {
48+
// WebSocket 세션 검증 제거 - REST API와 WebSocket 순서 무관하게 동작
4849
WebSocketSessionInfo sessionInfo = redisSessionStore.getUserSession(userId);
4950

50-
if (sessionInfo == null) {
51-
log.warn("세션 정보가 없어 방 입장 실패 - 사용자: {}, 방: {}", userId, roomId);
52-
throw new CustomException(ErrorCode.WS_SESSION_NOT_FOUND);
53-
}
54-
55-
if (sessionInfo.currentRoomId() != null && !sessionInfo.currentRoomId().equals(roomId)) {
51+
// 기존 방에서 퇴장 처리 (세션이 있고, 다른 방에 있는 경우만)
52+
if (sessionInfo != null && sessionInfo.currentRoomId() != null
53+
&& !sessionInfo.currentRoomId().equals(roomId)) {
5654
exitRoom(userId, sessionInfo.currentRoomId());
5755
log.debug("기존 방에서 퇴장 처리 완료 - 사용자: {}, 이전 방: {}",
5856
userId, sessionInfo.currentRoomId());
5957
}
6058

61-
WebSocketSessionInfo updatedSession = sessionInfo.withRoomId(roomId);
62-
redisSessionStore.saveUserSession(userId, updatedSession);
59+
// 세션 정보 업데이트 (세션이 있는 경우만)
60+
if (sessionInfo != null) {
61+
WebSocketSessionInfo updatedSession = sessionInfo.withRoomId(roomId);
62+
redisSessionStore.saveUserSession(userId, updatedSession);
63+
log.debug("WebSocket 세션 업데이트 완료 - 사용자: {}, 방: {}", userId, roomId);
64+
} else {
65+
log.debug("WebSocket 세션 없이 Redis 등록 - 사용자: {}, 방: {} (REST API 우선 호출)", userId, roomId);
66+
}
67+
68+
// Redis에 방 참가자 등록 (세션 없이도 가능!)
6369
redisSessionStore.addUserToRoom(roomId, userId);
6470

6571
// 아바타 정보 저장

src/test/java/com/back/global/websocket/service/RoomParticipantServiceTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,24 @@ void handleSessionDisconnected_ExitsAllRooms() {
156156
}
157157

158158
@Test
159-
@DisplayName("방 입장 - 세션 정보 없음 (예외 발생)")
160-
void enterRoom_NoSession_ThrowsException() {
159+
@DisplayName("방 입장 - 세션 정보 없음 (REST API 우선 호출, 정상 동작)")
160+
void enterRoom_NoSession_SuccessWithoutSession() {
161161
// given
162162
given(redisSessionStore.getUserSession(userId)).willReturn(null);
163+
given(userRepository.findById(userId)).willReturn(Optional.of(testUser));
163164

164-
// when & then
165-
assertThatThrownBy(() -> roomParticipantService.enterRoom(userId, roomId))
166-
.isInstanceOf(CustomException.class)
167-
.hasFieldOrPropertyWithValue("errorCode", ErrorCode.WS_SESSION_NOT_FOUND);
165+
// when
166+
roomParticipantService.enterRoom(userId, roomId);
168167

169-
verify(redisSessionStore, never()).addUserToRoom(anyLong(), anyLong());
170-
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
168+
// then
169+
// 세션 업데이트는 건너뛰지만, Redis 등록은 실행됨
170+
verify(redisSessionStore, never()).saveUserSession(eq(userId), any(WebSocketSessionInfo.class));
171+
verify(redisSessionStore).addUserToRoom(roomId, userId);
172+
173+
// 브로드캐스트는 정상 실행
174+
ArgumentCaptor<UserJoinedEvent> eventCaptor = ArgumentCaptor.forClass(UserJoinedEvent.class);
175+
verify(messagingTemplate).convertAndSend(eq("/topic/room/" + roomId + "/events"), eventCaptor.capture());
176+
assertThat(eventCaptor.getValue().getUserId()).isEqualTo(userId);
171177
}
172178

173179
@Test

0 commit comments

Comments
 (0)