Skip to content

Commit

Permalink
Merge pull request #144 from KAKAO-TOUR-API-CONTEST/devyj
Browse files Browse the repository at this point in the history
fix : sse string -> json
  • Loading branch information
yyujin1231 authored Oct 13, 2024
2 parents 2ca6bd5 + e2500a8 commit 792d4be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 32 additions & 8 deletions src/main/java/com/example/ai_jeju/service/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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);
Expand All @@ -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<String, String> 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<Long> allUserIds = emitterRepository.findAllUserIds();
for (Long userId : allUserIds) {
sendMessage(userId, messageDto.getMessage());
sendMessage(userId, messageDto);
}
}
}

0 comments on commit 792d4be

Please sign in to comment.