diff --git a/src/main/java/com/catchroom/chat/global/handler/StompHandler.java b/src/main/java/com/catchroom/chat/global/handler/StompHandler.java index 1dfe7ec..6dfc8ff 100644 --- a/src/main/java/com/catchroom/chat/global/handler/StompHandler.java +++ b/src/main/java/com/catchroom/chat/global/handler/StompHandler.java @@ -51,11 +51,11 @@ public Message preSend(Message message, MessageChannel channel) { String name = chatRoomRepository.getUserInfoBySessionId(sessionId); // 채팅방의 인원수를 -1한다. - chatRoomRepository.minusUserCount(roomId); - - chatService.sendChatMessage( - ChatMessageDto.builder().type(MessageType.QUIT).roomId(roomId).sender(name).build() - ); +// chatRoomRepository.minusUserCount(roomId); +// +// chatService.sendChatMessage( +// ChatMessageDto.builder().type(MessageType.QUIT).roomId(roomId).sender(name).build() +// ); // 퇴장한 클라이언트의 roomId 맵핑 정보를 삭제한다. chatRoomRepository.removeUserEnterInfo(sessionId); diff --git a/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java b/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java index 50b6d10..4bd6a77 100644 --- a/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java +++ b/src/main/java/com/catchroom/chat/global/pubsub/RedisSubscriber.java @@ -1,12 +1,16 @@ package com.catchroom.chat.global.pubsub; +import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; import com.catchroom.chat.message.dto.ChatMessageDto; +import com.catchroom.chat.message.dto.MessageSubDto; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.stereotype.Service; +import java.util.List; + @Slf4j @RequiredArgsConstructor @Service @@ -24,7 +28,8 @@ public void sendMessage(String publishMessage) { log.info("Redis Subcriber publishMsg: {}", publishMessage); // ChatMessage 객채로 맵핑 - ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, ChatMessageDto.class); +// ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, ChatMessageDto.class); + ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, MessageSubDto.class).getChatMessageDto(); // 채팅방을 구독한 클라이언트에게 메시지 발송 messagingTemplate.convertAndSend("/sub/chat/room/" + chatMessage.getRoomId(), chatMessage); @@ -33,13 +38,18 @@ public void sendMessage(String publishMessage) { } } - public void sendRoomList(String message) { + public void sendRoomList(String publishMessage) { try { - log.info("Redis Subcriber room publishMsg: {}", message); - // ChatMessage 객채로 맵핑 - ChatMessageDto chatMessage = objectMapper.readValue(message, ChatMessageDto.class); + log.info("Redis Subcriber room publishMsg: {}", publishMessage); + + + ChatMessageDto chatMessage = objectMapper.readValue(publishMessage, MessageSubDto.class).getChatMessageDto(); + List chatRoomListGetResponseList = objectMapper.readValue(publishMessage, MessageSubDto.class) + .getList(); + + // 채팅방을 구독한 클라이언트에게 메시지 발송 - messagingTemplate.convertAndSend("/sub/chat/room/" + chatMessage.getUserId(), chatMessage); + messagingTemplate.convertAndSend("/sub/chat/roomlist/" + chatMessage.getUserId(), chatRoomListGetResponseList); } catch (Exception e) { log.error("Exception {}", e); } diff --git a/src/main/java/com/catchroom/chat/message/controller/ChatController.java b/src/main/java/com/catchroom/chat/message/controller/ChatController.java index 5c1b9cb..1fefc2b 100644 --- a/src/main/java/com/catchroom/chat/message/controller/ChatController.java +++ b/src/main/java/com/catchroom/chat/message/controller/ChatController.java @@ -9,6 +9,7 @@ import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestHeader; @RequiredArgsConstructor @Controller @@ -23,10 +24,12 @@ public class ChatController { * websocket "/pub/chat/message"로 들어오는 메시징을 처리한다. */ @MessageMapping("/chat/message") - public void message(ChatMessageDto message) { + public void message(ChatMessageDto message, + @RequestHeader("Authorization") String accessToken + ) { log.info("chatController Sender: {}", message.getSender()); ChatMessageDto chatMessageDto = chatMongoService.save(message); - chatService.sendChatMessage(chatMessageDto); + chatService.sendChatMessage(chatMessageDto, accessToken); } /** diff --git a/src/main/java/com/catchroom/chat/message/dto/MessageSubDto.java b/src/main/java/com/catchroom/chat/message/dto/MessageSubDto.java new file mode 100644 index 0000000..76dbe20 --- /dev/null +++ b/src/main/java/com/catchroom/chat/message/dto/MessageSubDto.java @@ -0,0 +1,17 @@ +package com.catchroom.chat.message.dto; + +import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.util.List; + +@Getter +@Setter +@Builder +public class MessageSubDto implements Serializable { + private ChatMessageDto chatMessageDto; + private List list; +} diff --git a/src/main/java/com/catchroom/chat/message/service/ChatService.java b/src/main/java/com/catchroom/chat/message/service/ChatService.java index dcead84..e35a467 100644 --- a/src/main/java/com/catchroom/chat/message/service/ChatService.java +++ b/src/main/java/com/catchroom/chat/message/service/ChatService.java @@ -1,10 +1,16 @@ package com.catchroom.chat.message.service; +import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; +import com.catchroom.chat.chatroom.service.ChatRoomService; +import com.catchroom.chat.feign.client.MainFeignClient; import com.catchroom.chat.message.dto.ChatMessageDto; import com.catchroom.chat.global.pubsub.RedisPublisher; +import com.catchroom.chat.message.dto.MessageSubDto; import com.catchroom.chat.message.repository.ChatRoomRepository; import com.catchroom.chat.message.type.MessageType; import java.time.LocalDateTime; +import java.util.List; + import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -14,6 +20,7 @@ public class ChatService { private final RedisPublisher redisPublisher; private final ChatRoomRepository chatRoomRepository; + private final ChatRoomService chatRoomService; /** * destination 정보에서 roomId 추출 @@ -29,10 +36,18 @@ public String getRoomId(String destination) { /** * 채팅방에 메시지 발송 */ - public void sendChatMessage(ChatMessageDto chatMessage) { + public void sendChatMessage(ChatMessageDto chatMessage, String accessToken) { chatMessage.setUserCount(chatRoomRepository.getUserCount(chatMessage.getRoomId())); chatRoomRepository.setLastChatMessage(chatMessage.getRoomId(), chatMessage); + List chatRoomListGetResponseList = + chatRoomService.getChatRoomList(accessToken); + + MessageSubDto messageSubDto = MessageSubDto.builder() + .chatMessageDto(chatMessage) + .list(chatRoomListGetResponseList) + .build(); + redisPublisher.publish(chatMessage); }