feat(user-creator-chat): WebSocket 퇴장과 heartbeat를 처리한다
This commit is contained in:
@@ -17,10 +17,17 @@ class UserCreatorChatWebSocketHandler(
|
||||
private val objectMapper: ObjectMapper
|
||||
) : TextWebSocketHandler() {
|
||||
override fun handleTextMessage(session: WebSocketSession, message: TextMessage) {
|
||||
val request = objectMapper.readValue(message.payload, UserCreatorChatWebSocketMessage::class.java)
|
||||
val request = try {
|
||||
objectMapper.readValue(message.payload, UserCreatorChatWebSocketMessage::class.java)
|
||||
} catch (e: Exception) {
|
||||
sendProtocolError(session)
|
||||
return
|
||||
}
|
||||
when (request.type) {
|
||||
UserCreatorChatWebSocketMessageType.JOIN_ROOM -> handleJoinRoom(session, request)
|
||||
UserCreatorChatWebSocketMessageType.SEND_TEXT -> handleSendText(session, request)
|
||||
UserCreatorChatWebSocketMessageType.LEAVE_ROOM -> handleLeaveRoom(session, request)
|
||||
UserCreatorChatWebSocketMessageType.PING -> handlePing(session, request)
|
||||
else -> sendError(session, request, "common.error.invalid_request")
|
||||
}
|
||||
}
|
||||
@@ -48,9 +55,7 @@ class UserCreatorChatWebSocketHandler(
|
||||
|
||||
private fun handleSendText(session: WebSocketSession, request: UserCreatorChatWebSocketMessage) {
|
||||
try {
|
||||
if (session.attributes[JOINED_ROOM_ID_ATTRIBUTE] != request.roomId) {
|
||||
throw SodaException(messageKey = "chat.room.join_required")
|
||||
}
|
||||
requireJoinedRoom(session, request.roomId)
|
||||
val textMessage = request.payload["textMessage"]?.asText().orEmpty()
|
||||
val message = service.sendTextMessageByWebSocket(
|
||||
memberId = memberId(session),
|
||||
@@ -63,6 +68,31 @@ class UserCreatorChatWebSocketHandler(
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleLeaveRoom(session: WebSocketSession, request: UserCreatorChatWebSocketMessage) {
|
||||
try {
|
||||
requireJoinedRoom(session, request.roomId)
|
||||
clearJoinedRoom(session)
|
||||
} catch (e: SodaException) {
|
||||
sendError(session, request, e.messageKey ?: "common.error.invalid_request")
|
||||
}
|
||||
}
|
||||
|
||||
private fun handlePing(session: WebSocketSession, request: UserCreatorChatWebSocketMessage) {
|
||||
try {
|
||||
requireJoinedRoom(session, request.roomId)
|
||||
presenceService.refresh(roomId = request.roomId, memberId = memberId(session), sessionId = session.id)
|
||||
sendEnvelope(
|
||||
session,
|
||||
UserCreatorChatWebSocketMessageType.PONG,
|
||||
request.requestId,
|
||||
request.roomId,
|
||||
emptyMap<String, Any>()
|
||||
)
|
||||
} catch (e: SodaException) {
|
||||
sendError(session, request, e.messageKey ?: "common.error.invalid_request")
|
||||
}
|
||||
}
|
||||
|
||||
override fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) {
|
||||
clearJoinedRoom(session)
|
||||
}
|
||||
@@ -80,6 +110,12 @@ class UserCreatorChatWebSocketHandler(
|
||||
sessionRegistry.remove(session.id)
|
||||
}
|
||||
|
||||
private fun requireJoinedRoom(session: WebSocketSession, roomId: Long) {
|
||||
if (session.attributes[JOINED_ROOM_ID_ATTRIBUTE] != roomId) {
|
||||
throw SodaException(messageKey = "chat.room.join_required")
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendError(session: WebSocketSession, request: UserCreatorChatWebSocketMessage, messageKey: String) {
|
||||
sendEnvelope(
|
||||
session,
|
||||
@@ -90,6 +126,16 @@ class UserCreatorChatWebSocketHandler(
|
||||
)
|
||||
}
|
||||
|
||||
private fun sendProtocolError(session: WebSocketSession) {
|
||||
sendEnvelope(
|
||||
session,
|
||||
UserCreatorChatWebSocketMessageType.ERROR,
|
||||
null,
|
||||
0L,
|
||||
mapOf("messageKey" to "common.error.invalid_request")
|
||||
)
|
||||
}
|
||||
|
||||
private fun sendEnvelope(
|
||||
session: WebSocketSession,
|
||||
type: UserCreatorChatWebSocketMessageType,
|
||||
|
||||
Reference in New Issue
Block a user