Skip to content

Commit

Permalink
Fix :: ChatRoom, Socket 수정 (#287)
Browse files Browse the repository at this point in the history
ChatRoom에 유저 정보를 업데이트한 후 저장하도록 수정했습니다. WebSocket 연결, 구독, 연결 해제를 간소화하는 리팩토링 수행

문제는 when 키워드에서 위에 작업이 아래로 전수되면서 로직이 작동하는게 문제였음 그래서 필요한 로직만 수행하도록 변경
  • Loading branch information
yeseong0412 authored Oct 11, 2024
1 parent fd44994 commit 2594800
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
}

0 comments on commit 2594800

Please sign in to comment.