From e2500a8f0b4556e9fe4188f90bdac05937d35a4f Mon Sep 17 00:00:00 2001 From: Yujin Lee Date: Mon, 14 Oct 2024 01:21:49 +0900 Subject: [PATCH] fix : sse string -> json --- .../ai_jeju/controller/ChatController.java | 7 ---- .../ai_jeju/service/NotificationService.java | 40 +++++++++++++++---- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/example/ai_jeju/controller/ChatController.java b/src/main/java/com/example/ai_jeju/controller/ChatController.java index 37b243d..8341d77 100644 --- a/src/main/java/com/example/ai_jeju/controller/ChatController.java +++ b/src/main/java/com/example/ai_jeju/controller/ChatController.java @@ -145,13 +145,6 @@ public SseEmitter subscribeToRoom(@PathVariable("roomId") Long roomId, @RequestP - @PostMapping("/send-data/{id}") - public void sendData(@PathVariable("roomId") Long id) { - notificationService.notify(id, "data"); - } - - - @GetMapping("/chatroom") public String chat(@RequestParam("roomId") String roomId, Model model) { diff --git a/src/main/java/com/example/ai_jeju/service/NotificationService.java b/src/main/java/com/example/ai_jeju/service/NotificationService.java index ec22a2f..082e0ce 100644 --- a/src/main/java/com/example/ai_jeju/service/NotificationService.java +++ b/src/main/java/com/example/ai_jeju/service/NotificationService.java @@ -6,13 +6,17 @@ import com.example.ai_jeju.repository.ChatMessageRepository; import com.example.ai_jeju.repository.ChatRoomRepository; import com.example.ai_jeju.repository.EmitterRepository; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -70,8 +74,20 @@ private void resendLastMessage(String roomId, Long userId) { if (lastMessage.isPresent()) { try { String message = lastMessage.get().getMessage(); + String sender = lastMessage.get().getSender(); + + log.info("Resending message. Sender: {}, Message: {}", sender, message); + + Map messageData = new HashMap<>(); + messageData.put("sender", sender); + messageData.put("message", message); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonMessage = objectMapper.writeValueAsString(messageData); log.info("Resending last message '{}' to user {} in room {}", message, userId, roomId); - emitter.send(SseEmitter.event().name("chatMessage").data(message)); + emitter.send(SseEmitter.event() + .name("ChatMessage") + .data(jsonMessage)); + } catch (IOException e) { log.error("Error resending last message to user with ID {}", userId, e); emitter.completeWithError(e); @@ -85,26 +101,34 @@ private void resendLastMessage(String roomId, Long userId) { } } - public void sendMessage(Long userId, String message) { + public void sendMessage(Long userId, ChatMessageDto messageDto) { SseEmitter emitter = emitterRepository.findByUserId(userId); if (emitter != null) { try { - log.info("Sending message '{}' to user with ID {}", message, userId); - emitter.send(SseEmitter.event().name("chatMessage").data(message)); + log.info("Sending message '{}' from '{}' to user with ID {}", messageDto.getMessage(), messageDto.getSender(), userId); + + + Map messageData = new HashMap<>(); + messageData.put("sender", messageDto.getSender()); + messageData.put("message", messageDto.getMessage()); + + + ObjectMapper objectMapper = new ObjectMapper(); + String jsonMessage = objectMapper.writeValueAsString(messageData); + + + emitter.send(SseEmitter.event().name("chatMessage").data(jsonMessage)); } catch (IOException e) { emitter.completeWithError(e); } } } - public void notify(Long userId, Object event) { - sendMessage(userId, event.toString()); - } public void notifyAllSubscribers(ChatMessageDto messageDto) { List allUserIds = emitterRepository.findAllUserIds(); for (Long userId : allUserIds) { - sendMessage(userId, messageDto.getMessage()); + sendMessage(userId, messageDto); } } }