Skip to content

Commit

Permalink
fix: broker 대체로 인한 컨트롤러 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kjungw1025 committed Jan 23, 2024
1 parent c2b245d commit 83db056
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.dku.council.domain.chat.controller;

import com.dku.council.domain.chat.model.MessageType;
import com.dku.council.domain.chat.model.dto.Message;
import com.dku.council.domain.chat.model.dto.request.RequestChatDto;
import com.dku.council.domain.chat.model.dto.response.ResponseChatDto;
import com.dku.council.domain.chat.service.ChatService;
import com.dku.council.domain.chat.service.MessageSender;
import com.dku.council.global.auth.role.UserAuth;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
Expand All @@ -27,13 +30,17 @@
@Slf4j
public class ChatController {

@Value("${spring.kafka.consumer.topic}")
private String topic;

/**
* 아래에서 사용되는 convertAndSend 를 사용하기 위한 선언
* convertAndSend 는 객체를 인자로 넘겨주면 자동으로 Message 객체로 변환 후 도착지로 전송한다.
*/
private final SimpMessageSendingOperations template;

private final ChatService chatService;
private final MessageSender sender;

/**
* 채팅방 별, 입장 이벤트 발생시 처리되는 기능
Expand All @@ -59,15 +66,14 @@ public void enterUser(@Payload RequestChatDto chat,
headerAccessor.getSessionAttributes().put("username", username);
headerAccessor.getSessionAttributes().put("roomId", chat.getRoomId());

ResponseChatDto responseChatDto = ResponseChatDto.builder()
Message message = Message.builder()
.type(chat.getType())
.roomId(chat.getRoomId())
.sender(chat.getSender())
.message(chat.getMessage())
.message(chat.getSender() + " 님 입장!!")
.build();
responseChatDto.changeMessage(responseChatDto.getSender() + " 님 입장!!");

template.convertAndSend("/sub/chatRoom/enter" + responseChatDto.getRoomId(), responseChatDto);
sender.send(topic, message);
}

/**
Expand All @@ -79,15 +85,14 @@ public void enterUser(@Payload RequestChatDto chat,
public void sendMessage(@Payload RequestChatDto chat) {
log.info("CHAT {}", chat);

ResponseChatDto responseChatDto = ResponseChatDto.builder()
Message message = Message.builder()
.type(chat.getType())
.roomId(chat.getRoomId())
.sender(chat.getSender())
.message(chat.getMessage())
.build();
responseChatDto.changeMessage(responseChatDto.getMessage());

template.convertAndSend("/sub/chatRoom/enter" + responseChatDto.getRoomId(), responseChatDto);
sender.send(topic, message);
}

/**
Expand Down Expand Up @@ -119,14 +124,14 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) {
log.info("User Disconnected : ", username);

// builder 어노테이션 활용
ResponseChatDto chat = ResponseChatDto.builder()
Message message = Message.builder()
.type(MessageType.LEAVE)
.sender(username)
.roomId(roomId)
.message(username + " 님 퇴장!!")
.build();

template.convertAndSend("/sub/chatRoom/enter" + roomId, chat);
sender.send(topic, message);
}
}

Expand Down

0 comments on commit 83db056

Please sign in to comment.