Skip to content

Commit

Permalink
Merge pull request #26 from catchroom/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
HyemIin authored Jan 19, 2024
2 parents a734820 + 8f54c60 commit 01db1d9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand All @@ -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<ChatRoomListGetResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/catchroom/chat/message/dto/MessageSubDto.java
Original file line number Diff line number Diff line change
@@ -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<ChatRoomListGetResponse> list;
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +20,7 @@ public class ChatService {

private final RedisPublisher redisPublisher;
private final ChatRoomRepository chatRoomRepository;
private final ChatRoomService chatRoomService;

/**
* destination ์ •๋ณด์—์„œ roomId ์ถ”์ถœ
Expand All @@ -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<ChatRoomListGetResponse> chatRoomListGetResponseList =
chatRoomService.getChatRoomList(accessToken);

MessageSubDto messageSubDto = MessageSubDto.builder()
.chatMessageDto(chatMessage)
.list(chatRoomListGetResponseList)
.build();

redisPublisher.publish(chatMessage);
}

Expand Down

0 comments on commit 01db1d9

Please sign in to comment.