Skip to content

Commit

Permalink
feat(GameChat): MessageController 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzseong committed Mar 14, 2024
1 parent 05503a4 commit 9e9aea9
Showing 1 changed file with 13 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
package com.sportsecho.gamechat.controller;

import com.sportsecho.gamechat.dto.GameChatRequestDto;
import com.sportsecho.gamechat.dto.GameChatResponseDto;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import com.sportsecho.common.kafka.KafkaConst;
import com.sportsecho.common.kafka.KafkaMessageUtil;
import com.sportsecho.gamechat.dto.MessageRequestDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RestController
@RequiredArgsConstructor
@Slf4j(topic = "MessageController")
public class MessageController {

@MessageMapping("/gameChat/{gameId}/enter")
@SendTo("/topic/{gameId}")
public GameChatResponseDto enter(
@DestinationVariable("gameId") Long gameId,
GameChatRequestDto request
) {
private final KafkaMessageUtil kafkaMessageUtil;

return GameChatResponseDto.builder()
.content(request.getSender() + "님이 입장하셨습니다.")
.build();
}
@MessageMapping("/message")
public void sendMessage(MessageRequestDto messageRequestDto) {
log.info("Received message\nsender = {}, message = {}", messageRequestDto.getSender(), messageRequestDto.getMessage());

@MessageMapping("/gameChat/{gameId}")
@SendTo("/topic/{gameId}")
public GameChatResponseDto publish(
@DestinationVariable("gameId") Long gameId,
GameChatRequestDto request
) {
LocalDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDateTime();

String formattedTime = now.format(DateTimeFormatter.ofPattern("HH:mm"));

return GameChatResponseDto.builder()
.sender(request.getSender())
.content(request.getMessage())
.sendAt(formattedTime)
.build();
//message service logic
kafkaMessageUtil.send(KafkaConst.KAFKA_TOPIC, messageRequestDto);
}

}


0 comments on commit 9e9aea9

Please sign in to comment.