From 538e3847ace5175b1b3542b6658d63b41ccecd1d Mon Sep 17 00:00:00 2001 From: david-parkk Date: Sat, 26 Oct 2024 20:46:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=EB=B2=84=EA=B7=B8=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/kutaverse/game/chat/domain/Chat.java | 4 +++ .../game/chat/service/ChatService.java | 7 +++- .../websocket/chat/ChatWebSocketHandler.java | 36 +++++++++++++------ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/kutaverse/game/chat/domain/Chat.java b/src/main/java/kutaverse/game/chat/domain/Chat.java index e5f9fd1..152cc0c 100644 --- a/src/main/java/kutaverse/game/chat/domain/Chat.java +++ b/src/main/java/kutaverse/game/chat/domain/Chat.java @@ -2,6 +2,8 @@ import lombok.AllArgsConstructor; import lombok.Builder; +import lombok.Getter; +import lombok.ToString; import lombok.experimental.WithBy; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @@ -11,6 +13,8 @@ @Document(collation = "chat") @AllArgsConstructor @Builder +@Getter +@ToString public class Chat { @Id diff --git a/src/main/java/kutaverse/game/chat/service/ChatService.java b/src/main/java/kutaverse/game/chat/service/ChatService.java index 58d2408..5d22dc5 100644 --- a/src/main/java/kutaverse/game/chat/service/ChatService.java +++ b/src/main/java/kutaverse/game/chat/service/ChatService.java @@ -18,9 +18,14 @@ public class ChatService { public Mono save(ChatRequestDto chatRequestDto){ Chat chat=Chat.builder() .senderUserId(chatRequestDto.getUserId()) + .nickname(chatRequestDto.getNickname()) .content(chatRequestDto.getContent()) .createdAt(LocalDateTime.now()) .build(); - return chatRepository.insert(chat); + return chatRepository.insert(chat) + .doOnNext(savedChat -> { + System.out.println("Saved chat: " + savedChat); + }); + } } diff --git a/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java b/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java index 57585f8..320a269 100644 --- a/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java +++ b/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java @@ -1,6 +1,7 @@ package kutaverse.game.websocket.chat; import com.fasterxml.jackson.databind.ObjectMapper; +import kutaverse.game.chat.domain.Chat; import kutaverse.game.chat.service.ChatService; import kutaverse.game.websocket.chat.dto.request.ChatRequestDto; import kutaverse.game.websocket.chat.dto.response.ChatResponseDto; @@ -9,6 +10,7 @@ import org.springframework.web.reactive.socket.WebSocketSession; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -26,6 +28,7 @@ public ChatWebSocketHandler(ChatService chatService){ public Mono handle(WebSocketSession session) { sessions.put(session.getId(), session); ObjectMapper objectMapper=new ObjectMapper(); + session.receive() .map(WebSocketMessage::getPayloadAsText) .map(data-> { @@ -36,19 +39,30 @@ public Mono handle(WebSocketSession session) { } }) .doOnNext(System.out::println) - .map(chatRequest -> { - chatService.save(chatRequest).subscribe(); - ChatResponseDto response = new ChatResponseDto(chatRequest.getUserId(), chatRequest.getNickname(), chatRequest.getContent()); - try { - return objectMapper.writeValueAsString(response); - } catch (Exception e) { - throw new RuntimeException(e); - } + .flatMap(chatRequest -> { + //chatService.save(chatRequest).subscribe(); + return chatService.save(chatRequest).map(savedChat -> { + // 저장된 결과를 기반으로 응답 생성 + System.out.println(savedChat.getId()); + ChatResponseDto response = new ChatResponseDto(savedChat.getSenderUserId(), savedChat.getNickname(), savedChat.getContent()); + try { + return objectMapper.writeValueAsString(response); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); }) .flatMap(data->{ - Flux.fromIterable(sessions.values()) - .flatMap(wsSession -> wsSession.send(Mono.just(wsSession.textMessage(data)))).subscribe(); - return Mono.never(); + return Flux.fromIterable(sessions.values()) + .flatMap(wsSession -> { + if(!wsSession.isOpen()) { + wsSession.close().subscribe(); + return Mono.never(); + } + else + return wsSession.send(Mono.just(wsSession.textMessage(data))); + }).then(); + //return Mono.never(); }) .subscribe(); From f849800e93a9c1f2e77a02b65ae9328dcd04388b Mon Sep 17 00:00:00 2001 From: david-parkk Date: Sat, 26 Oct 2024 20:54:22 +0900 Subject: [PATCH 2/2] fix: bug --- .../game/websocket/chat/ChatWebSocketHandler.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java b/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java index 320a269..065865a 100644 --- a/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java +++ b/src/main/java/kutaverse/game/websocket/chat/ChatWebSocketHandler.java @@ -53,10 +53,13 @@ public Mono handle(WebSocketSession session) { }); }) .flatMap(data->{ - return Flux.fromIterable(sessions.values()) - .flatMap(wsSession -> { + return Flux.fromIterable(sessions.entrySet()) + .flatMap(entry -> { + WebSocketSession wsSession = entry.getValue(); + String key = entry.getKey(); if(!wsSession.isOpen()) { wsSession.close().subscribe(); + sessions.remove(key); return Mono.never(); } else