diff --git a/.gitignore b/.gitignore index 384002b6..6c80bf70 100644 --- a/.gitignore +++ b/.gitignore @@ -10,8 +10,10 @@ confluent-hub-client-latest.tar.gz ./spring-chatting-auth-server/build ./spring-chatting-auth-server/build - - +/.idea +/app +/backups +/kafka-connect-jdbc .idea/ *.iml *.iws \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4e8b0a89..645e4aa2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,8 +5,8 @@ + - @@ -123,7 +123,7 @@ - + diff --git a/spring-chatting-backend-server/src/main/java/chatting/chat/domain/data/Chatting.java b/spring-chatting-backend-server/src/main/java/chatting/chat/domain/data/Chatting.java index f520ef99..a7788c9a 100644 --- a/spring-chatting-backend-server/src/main/java/chatting/chat/domain/data/Chatting.java +++ b/spring-chatting-backend-server/src/main/java/chatting/chat/domain/data/Chatting.java @@ -14,7 +14,6 @@ public class Chatting { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long id; - @ManyToOne @JoinColumn(name = "ROOM_ID") private Room room; diff --git a/spring-chatting-backend-server/src/main/java/chatting/chat/web/ChatController.java b/spring-chatting-backend-server/src/main/java/chatting/chat/web/ChatController.java index 5f51ca94..b4fe5085 100644 --- a/spring-chatting-backend-server/src/main/java/chatting/chat/web/ChatController.java +++ b/spring-chatting-backend-server/src/main/java/chatting/chat/web/ChatController.java @@ -91,7 +91,9 @@ public ResponseEntity findFriend(@RequestParam("userId") String userId){ public ResponseEntity findChatRecords(@RequestParam("roomId") Long roomId){ Room findRoom = roomService.findByRoomId(roomId); List findChattings = chatService.findAllByRoomId(findRoom.getRoomId()); - return ResponseEntity.ok(findChattings); + List response = findChattings.stream().map(c -> new ChatRecord(c.getId(), c.getRoom().getRoomId(), c.getSendUser().getUserId(), c.getSendUser().getUserName(), c.getMessage(), c.getCreatedDate(), c.getCreatedTime())).collect(Collectors.toList()); + + return ResponseEntity.ok(response); } // 특정 채팅 조회 diff --git a/spring-chatting-backend-server/src/main/java/chatting/chat/web/kafka/dto/ChatRecord.java b/spring-chatting-backend-server/src/main/java/chatting/chat/web/kafka/dto/ChatRecord.java new file mode 100644 index 00000000..825fc7b6 --- /dev/null +++ b/spring-chatting-backend-server/src/main/java/chatting/chat/web/kafka/dto/ChatRecord.java @@ -0,0 +1,30 @@ +package chatting.chat.web.kafka.dto; + + +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Getter +@Setter +public class ChatRecord { + private Long id; + private Long roomId; + private String sendUserId; + private String sendUserName; + private String message; + private LocalDate createdDate; + private LocalTime createdTime; + + public ChatRecord(Long id, Long roomId, String sendUserId, String sendUserName, String message, LocalDate createdDate, LocalTime createdTime) { + this.id = id; + this.roomId = roomId; + this.sendUserId = sendUserId; + this.sendUserName = sendUserName; + this.message = message; + this.createdDate = createdDate; + this.createdTime = createdTime; + } +} diff --git a/spring-chatting-front-server/src/main/java/chatting/chat/web/dto/ChatRecord.java b/spring-chatting-front-server/src/main/java/chatting/chat/web/dto/ChatRecord.java new file mode 100644 index 00000000..dcb91170 --- /dev/null +++ b/spring-chatting-front-server/src/main/java/chatting/chat/web/dto/ChatRecord.java @@ -0,0 +1,33 @@ +package chatting.chat.web.dto; + + +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Getter +@Setter +public class ChatRecord { + private Long id; + private Long roomId; + private String sendUserId; + private String sendUserName; + private String message; + private LocalDate createdDate; + private LocalTime createdTime; + + public ChatRecord() { + } + + public ChatRecord(Long id, Long roomId, String sendUserId, String sendUserName, String message, LocalDate createdDate, LocalTime createdTime) { + this.id = id; + this.roomId = roomId; + this.sendUserId = sendUserId; + this.sendUserName = sendUserName; + this.message = message; + this.createdDate = createdDate; + this.createdTime = createdTime; + } +} diff --git a/spring-chatting-front-server/src/main/java/chatting/chat/web/kafka/dto/RequestAddChatMessageDTO.java b/spring-chatting-front-server/src/main/java/chatting/chat/web/kafka/dto/RequestAddChatMessageDTO.java index c8e6a2f6..5ab95c06 100644 --- a/spring-chatting-front-server/src/main/java/chatting/chat/web/kafka/dto/RequestAddChatMessageDTO.java +++ b/spring-chatting-front-server/src/main/java/chatting/chat/web/kafka/dto/RequestAddChatMessageDTO.java @@ -10,4 +10,14 @@ public class RequestAddChatMessageDTO { private String writer; private String writerId; private String message; + + public RequestAddChatMessageDTO() { + } + + public RequestAddChatMessageDTO(Long roomId, String writer, String writerId, String message) { + this.roomId = roomId; + this.writer = writer; + this.writerId = writerId; + this.message = message; + } } \ No newline at end of file diff --git a/spring-chatting-front-server/src/main/java/chatting/chat/web/user/UserController.java b/spring-chatting-front-server/src/main/java/chatting/chat/web/user/UserController.java index df3ce608..d6788b53 100644 --- a/spring-chatting-front-server/src/main/java/chatting/chat/web/user/UserController.java +++ b/spring-chatting-front-server/src/main/java/chatting/chat/web/user/UserController.java @@ -230,26 +230,59 @@ public String createRoomForm(@SessionAttribute(name = SessionConst.LOGIN_MEMBER, return "redirect:/user/rooms"; } + // 패킷의 크기 또한 신경써야될듯 // 채팅방 @GetMapping("/chat") - public String chattingRoom(@RequestParam Long roomId, Model model, HttpSession session) { - User user = (User) session.getAttribute(SessionConst.LOGIN_MEMBER); + public String chattingRoom(@SessionAttribute(name = SessionConst.LOGIN_MEMBER, required = true) User user, + @RequestParam Long roomId, @RequestParam String roomName, Model model) { + + try { + + Flux response = webClient.mutate() + .build() + .get() + .uri("/chat/chats?roomId="+roomId) + .retrieve() + .onStatus( + HttpStatus::is4xxClientError, + r -> r.bodyToMono(ErrorResponse.class).map(CustomThrowableException::new)) + .bodyToFlux(ChatRecord.class); - // Participant에서 Room참여자들 정보를 가져와 모델에 전달 - Participant findParticipant = userService.findByRoomIdAndUserId(roomId,user.getUserId()); - model.addAttribute("user", user); + List records = response.collect(Collectors.toList()) + .share().block(); - // 유저가 참여하고있는 채팅방정보를 DTO에 담아 모델에 전달 - RoomInfoDTO roomInfoDTO = new RoomInfoDTO(); - roomInfoDTO.setName(findParticipant.getRoomName()); - roomInfoDTO.setRoomId(roomId); - model.addAttribute("room", roomInfoDTO); + model.addAttribute("roomId",roomId); + model.addAttribute("roomName",roomName); + model.addAttribute("user", user); + model.addAttribute("records",records); - // 입장한 채팅방의 채팅메세지들을 가져와 모델에 전달 - List findChattings = chatService.findAllByRoomId(roomId); - model.addAttribute("records",findChattings); + }catch (CustomThrowableException e){ + log.info(e.getErrorResponse().getCode()); + log.info(e.getErrorResponse().getMessage()); + return "users/chat"; + } - return "/users/chat"; + return "users/chat"; + + + +// +// +// // Participant에서 Room참여자들 정보를 가져와 모델에 전달 +// Participant findParticipant = userService.findByRoomIdAndUserId(roomId,user.getUserId()); +// model.addAttribute("user", user); +// +// // 유저가 참여하고있는 채팅방정보를 DTO에 담아 모델에 전달 +// RoomInfoDTO roomInfoDTO = new RoomInfoDTO(); +// roomInfoDTO.setName(findParticipant.getRoomName()); +// roomInfoDTO.setRoomId(roomId); +// model.addAttribute("room", roomInfoDTO); +// +// // 입장한 채팅방의 채팅메세지들을 가져와 모델에 전달 +// List findChattings = chatService.findAllByRoomId(roomId); +// model.addAttribute("records",findChattings); +// +// return "/users/chat"; } diff --git a/spring-chatting-front-server/src/main/java/chatting/chat/web/websocket/StompChatController.java b/spring-chatting-front-server/src/main/java/chatting/chat/web/websocket/StompChatController.java index b931cbe1..3fc1bfab 100644 --- a/spring-chatting-front-server/src/main/java/chatting/chat/web/websocket/StompChatController.java +++ b/spring-chatting-front-server/src/main/java/chatting/chat/web/websocket/StompChatController.java @@ -1,17 +1,40 @@ package chatting.chat.web.websocket; import chatting.chat.web.dto.ChatMessage; +import chatting.chat.web.dto.ChatRecord; +import chatting.chat.web.dto.RequestAddChatRoomDTO; +import chatting.chat.web.error.CustomThrowableException; +import chatting.chat.web.error.ErrorResponse; +import chatting.chat.web.kafka.dto.RequestAddChatMessageDTO; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.stereotype.Controller; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; + +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.stream.Collectors; @Controller @RequiredArgsConstructor @Slf4j public class StompChatController { + private WebClient webClient; + + @Value("${backend.api.gateway}") + private String backEntry; + + @PostConstruct + public void initWebClient() { + this.webClient = WebClient.create(backEntry); + } + // STOMP 템플릿 private final SimpMessagingTemplate template; @@ -26,6 +49,25 @@ public void enter(ChatMessage message){ @MessageMapping(value = "/chat/message") public void message(ChatMessage message){ log.info(message.getMessage()); + + try { + webClient.mutate() + .build() + .post() + .uri("/chat/chat") + .bodyValue(new RequestAddChatMessageDTO(message.getRoomId(), message.getWriter(), message.getWriterId(), message.getMessage())) + .retrieve() + .onStatus( + HttpStatus::is4xxClientError, + r -> r.bodyToMono(ErrorResponse.class).map(CustomThrowableException::new)) + .bodyToMono(String.class).block(); + + }catch (CustomThrowableException e){ + log.info(e.getErrorResponse().getCode()); + log.info(e.getErrorResponse().getMessage()); + } + + template.convertAndSend("/sub/chat/room/" + message.getRoomId(), message); // Direct send topic to stomp // kafka topic send } } \ No newline at end of file diff --git a/spring-chatting-front-server/src/main/resources/static/css/msg.css b/spring-chatting-front-server/src/main/resources/static/css/msg.css index 6b5f5a93..6b91b73c 100644 --- a/spring-chatting-front-server/src/main/resources/static/css/msg.css +++ b/spring-chatting-front-server/src/main/resources/static/css/msg.css @@ -1,7 +1,7 @@ .msg-r{ - background-color: #fae4a7; + background-color: #d4ffcf; box-shadow: 0px 2px 2.2px #818181; padding-right: 5px; padding-left: 5px; @@ -11,11 +11,11 @@ text-align: right; border-radius: 5px; margin-left: auto; - margin-right: 0; + margin-right: 10px; } .msg-l{ - background-color: #ffffff; + background-color: #fff8d6; box-shadow: 0px 2px 2.2px #818181; padding-right: 5px; padding-left: 5px; @@ -24,7 +24,7 @@ width: fit-content; text-align: right; border-radius: 5px; - margin-left: 0; + margin-left: 10px; margin-right: auto; } @@ -46,6 +46,11 @@ grid-auto-flow: row; } +.m-tb-5{ + margin-top: 10px; + margin-bottom: 10px; +} + .item-h-50{ max-height: 300px; } diff --git a/spring-chatting-front-server/src/main/resources/templates/users/chat.html b/spring-chatting-front-server/src/main/resources/templates/users/chat.html index 430d33f0..6a084398 100644 --- a/spring-chatting-front-server/src/main/resources/templates/users/chat.html +++ b/spring-chatting-front-server/src/main/resources/templates/users/chat.html @@ -2,9 +2,9 @@ - - @@ -18,14 +18,12 @@
-

[[${room.name}]]

+

[[${roomName}]]

-
- Message Area -
+
@@ -36,7 +34,7 @@

[[${room.name}]]

To leave this room, - Click here + Click here
@@ -51,40 +49,29 @@

[[${room.name}]]