diff --git a/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt b/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt index ba7fb53a4..011b24269 100644 --- a/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt +++ b/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt @@ -60,6 +60,7 @@ class ChatRoomServiceImpl( val chatRoom = findChatRoomById(roomId) chatRoom.joinedUserInfo.find { it.userId == userId }?.timestamp = DateTimeUtil.localDateTime + chatRoomRepository.save(chatRoom).joinedUserInfo.find { it.userId == userId }?.timestamp } @Transactional @@ -69,6 +70,7 @@ class ChatRoomServiceImpl( val chatRoom = findChatRoomById(roomId) chatRoom.joinedUserInfo.find { it.userId == userId }?.timestamp = LocalDateTime.now() + chatRoomRepository.save(chatRoom).joinedUserInfo.find { it.userId == userId }?.timestamp } private fun toResponse(chatRoomEntity: ChatRoomEntity, userId: Long): RoomResponse { diff --git a/src/main/kotlin/com/seugi/api/domain/chat/presentation/websocket/config/StompWebSocketConfig.kt b/src/main/kotlin/com/seugi/api/domain/chat/presentation/websocket/config/StompWebSocketConfig.kt index 1c2cbf795..bc25ff7e1 100644 --- a/src/main/kotlin/com/seugi/api/domain/chat/presentation/websocket/config/StompWebSocketConfig.kt +++ b/src/main/kotlin/com/seugi/api/domain/chat/presentation/websocket/config/StompWebSocketConfig.kt @@ -50,64 +50,56 @@ class StompWebSocketConfig( val accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor::class.java)!! when (accessor.messageType) { - SimpMessageType.CONNECT -> { - val authToken = accessor.getNativeHeader("Authorization")?.firstOrNull() - - if (authToken != null && authToken.startsWith("Bearer ")) { - val auth = jwtUtils.getAuthentication(authToken) - - val userDetails = auth.principal as? JwtUserDetails - - val userId: String? = userDetails?.id?.value?.toString() - - if (userId != null) { - val simpAttributes = SimpAttributesContextHolder.currentAttributes() - simpAttributes.setAttribute("user-id", userId) - - return MessageBuilder.createMessage(message.payload, accessor.messageHeaders) - } else { - throw CustomException(MemberErrorCode.MEMBER_NOT_FOUND) - } - } - } - - SimpMessageType.CONNECT_ACK, - SimpMessageType.MESSAGE, - SimpMessageType.SUBSCRIBE, - -> { - if (accessor.destination != null) { - val simpAttributes = SimpAttributesContextHolder.currentAttributes() - val userId = simpAttributes.getAttribute("user-id") as String - chatRoomService.sub( - userId = userId.toLong(), - roomId = accessor.destination?.substringAfterLast(".").toString() - ) - } - } - - SimpMessageType.UNSUBSCRIBE, - SimpMessageType.HEARTBEAT, - SimpMessageType.DISCONNECT, - -> { - if (accessor.destination != null) { - val simpAttributes = SimpAttributesContextHolder.currentAttributes() - val userId = simpAttributes.getAttribute("user-id") as String - chatRoomService.unSub( - userId = userId.toLong(), - roomId = accessor.destination?.substringAfterLast(".").toString() - ) - } - } - SimpMessageType.DISCONNECT_ACK, - SimpMessageType.OTHER, - null, - -> { - } + SimpMessageType.CONNECT -> handleConnect(message, accessor) + SimpMessageType.SUBSCRIBE -> handleSubscribe(accessor) + SimpMessageType.UNSUBSCRIBE, SimpMessageType.DISCONNECT -> handleUnsubscribeOrDisconnect() + else -> {} } - return message - } }) } + + private fun handleConnect(message: Message<*>, accessor: StompHeaderAccessor) { + val authToken = accessor.getNativeHeader("Authorization")?.firstOrNull() + if (authToken != null && authToken.startsWith("Bearer ")) { + val auth = jwtUtils.getAuthentication(authToken) + val userDetails = auth.principal as? JwtUserDetails + val userId: String? = userDetails?.id?.value?.toString() + + if (userId != null) { + val simpAttributes = SimpAttributesContextHolder.currentAttributes() + simpAttributes.setAttribute("user-id", userId) + MessageBuilder.createMessage(message.payload, accessor.messageHeaders) + } else { + throw CustomException(MemberErrorCode.MEMBER_NOT_FOUND) + } + } + } + + private fun handleSubscribe(accessor: StompHeaderAccessor) { + accessor.destination?.let { + val simpAttributes = SimpAttributesContextHolder.currentAttributes() + simpAttributes.setAttribute("sub", it.substringAfterLast(".")) + val userId = simpAttributes.getAttribute("user-id") as String + chatRoomService.sub( + userId = userId.toLong(), + roomId = it.substringAfterLast(".") + ) + } + } + + private fun handleUnsubscribeOrDisconnect() { + val simpAttributes = SimpAttributesContextHolder.currentAttributes() + val userId = simpAttributes.getAttribute("user-id") as String? + val roomId = simpAttributes.getAttribute("sub") as String? + userId?.let { + roomId?.let { + chatRoomService.unSub( + userId = userId.toLong(), + roomId = it + ) + } + } + } } \ No newline at end of file