diff --git a/src/main/java/com/dku/council/domain/chat/controller/ChatController.java b/src/main/java/com/dku/council/domain/chat/controller/ChatController.java index b1b0308f..356d9689 100644 --- a/src/main/java/com/dku/council/domain/chat/controller/ChatController.java +++ b/src/main/java/com/dku/council/domain/chat/controller/ChatController.java @@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.socket.messaging.SessionDisconnectEvent; +import java.time.LocalDateTime; import java.util.List; @Tag(name = "채팅", description = "채팅 송/수신 관련 api") @@ -66,9 +67,6 @@ public void enterUser(@Payload RequestChatDto chat, // 채팅방에 유저 추가 및 UserUUID 반환 String username = chatService.addUser(chat.getRoomId(), chat.getSender()); - log.info("enterUser에서 uuid " + username); - log.info("enterUser에서 roomId " + chat.getRoomId()); - log.info("enterUser에서 userId " + chat.getUserId()); // 반환 결과를 socket session 에 userUUID 로 저장 headerAccessor.getSessionAttributes().put("username", username); @@ -76,14 +74,17 @@ public void enterUser(@Payload RequestChatDto chat, headerAccessor.getSessionAttributes().put("roomId", chat.getRoomId()); String enterMessage = chat.getSender() + " 님 입장!!"; + LocalDateTime messageTime = LocalDateTime.now(); + // 입장 메시지 저장 - chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), enterMessage); + chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), enterMessage, messageTime); Message message = Message.builder() .type(chat.getType()) .roomId(chat.getRoomId()) .sender(chat.getSender()) .message(enterMessage) + .messageTime(messageTime) .build(); sender.send(topic, message); @@ -96,15 +97,16 @@ public void enterUser(@Payload RequestChatDto chat, */ @MessageMapping("/chat/sendMessage") public void sendMessage(@Payload RequestChatDto chat) { - log.info("CHAT {}", chat); + LocalDateTime messageTime = LocalDateTime.now(); - chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), chat.getMessage()); + chatRoomMessageService.create(chat.getRoomId(), chat.getType().toString(), chat.getUserId(), chat.getSender(), chat.getMessage(), messageTime); Message message = Message.builder() .type(chat.getType()) .roomId(chat.getRoomId()) .sender(chat.getSender()) .message(chat.getMessage()) + .messageTime(messageTime) .build(); sender.send(topic, message); @@ -140,14 +142,17 @@ public void webSocketDisconnectListener(SessionDisconnectEvent event) { log.info("User Disconnected : ", username); String exitMessage = username + " 님 퇴장!!"; + LocalDateTime messageTime = LocalDateTime.now(); + // 퇴장 메시지 저장 - chatRoomMessageService.create(roomId, MessageType.LEAVE.toString(), userId, username, exitMessage); + chatRoomMessageService.create(roomId, MessageType.LEAVE.toString(), userId, username, exitMessage, messageTime); // builder 어노테이션 활용 Message message = Message.builder() .type(MessageType.LEAVE) .sender(username) .roomId(roomId) .message(exitMessage) + .messageTime(messageTime) .build(); sender.send(topic, message); diff --git a/src/main/java/com/dku/council/domain/chat/model/dto/Message.java b/src/main/java/com/dku/council/domain/chat/model/dto/Message.java index 545848db..dba682af 100644 --- a/src/main/java/com/dku/council/domain/chat/model/dto/Message.java +++ b/src/main/java/com/dku/council/domain/chat/model/dto/Message.java @@ -7,6 +7,7 @@ import lombok.ToString; import javax.validation.constraints.NotNull; +import java.time.LocalDateTime; @Getter @NoArgsConstructor @@ -24,14 +25,19 @@ public class Message { @NotNull private String message; + @NotNull + private LocalDateTime messageTime; + @Builder private Message(MessageType type, String roomId, String sender, - String message) { + String message, + LocalDateTime messageTime) { this.type = type; this.roomId = roomId; this.sender = sender; this.message = message; + this.messageTime = messageTime; } } diff --git a/src/main/java/com/dku/council/domain/chatmessage/service/ChatRoomMessageService.java b/src/main/java/com/dku/council/domain/chatmessage/service/ChatRoomMessageService.java index ceceb31f..eb553566 100644 --- a/src/main/java/com/dku/council/domain/chatmessage/service/ChatRoomMessageService.java +++ b/src/main/java/com/dku/council/domain/chatmessage/service/ChatRoomMessageService.java @@ -23,7 +23,8 @@ public void create(String roomId, String messageType, Long userId, String userNickname, - String content) { + String content, + LocalDateTime messageTime) { ChatRoomMessage chatRoomMessage = new ChatRoomMessage(); chatRoomMessage.setRoomId(roomId); @@ -31,7 +32,7 @@ public void create(String roomId, chatRoomMessage.setUserId(userId); chatRoomMessage.setUserNickname(userNickname); chatRoomMessage.setContent(content); - chatRoomMessage.setCreatedAt(LocalDateTime.now().atZone(seoulZoneId).toLocalDateTime()); + chatRoomMessage.setCreatedAt(messageTime.atZone(seoulZoneId).toLocalDateTime()); chatRoomMessageRepository.save(chatRoomMessage); } diff --git a/src/main/resources/static/js/chatroom/socket.js b/src/main/resources/static/js/chatroom/socket.js index 2c3cd27c..75d764c9 100644 --- a/src/main/resources/static/js/chatroom/socket.js +++ b/src/main/resources/static/js/chatroom/socket.js @@ -107,7 +107,7 @@ function getPreviousMessageList() { sender: data[i]["userNickname"], message: data[i]["content"], type: data[i]["messageType"], - createdAt: data[i]["createdAt"] + messageTime: data[i]["createdAt"] }; console.log(previousChatMessage); previousMessageReceived(JSON.stringify(previousChatMessage)); @@ -159,18 +159,24 @@ function previousMessageReceived(message) { } function addMessageToTheChatRoom(chat, messageElement) { - if (chat.type === 'ENTER' || chat.type === 'LEAVE') { // chatType 이 enter 라면 아래 내용 + if (chat.type === 'ENTER' || chat.type === 'LEAVE') { // chatType 이 enter, leave 라면 아래 내용 messageElement.classList.add('event-message'); getUserList(); } else { // chatType 이 talk 라면 아래 내용 messageElement.classList.add('chat-message'); + let timeElement = document.createElement('span'); + let timeText = document.createTextNode(formatLocalDateTime(chat.messageTime)); + timeElement.appendChild(timeText); + messageElement.appendChild(timeElement); + + messageElement.appendChild(document.createElement('br')); + let avatarElement = document.createElement('i'); let avatarText = document.createTextNode(chat.sender[0]); avatarElement.appendChild(avatarText); avatarElement.style['background-color'] = getAvatarColor(chat.sender); - messageElement.appendChild(avatarElement); let usernameElement = document.createElement('span'); @@ -223,6 +229,25 @@ function getAvatarColor(messageSender) { return colors[index]; } +function formatLocalDateTime(dateTimeString) { + // 주어진 문자열을 Date 객체로 변환 + const dateTime = new Date(dateTimeString); + + // 시간대 추출 + const hours = dateTime.getHours(); + const minutes = dateTime.getMinutes(); + + // 오전/오후 구분 + const meridiem = hours >= 12 ? "오후" : "오전"; + + // 시간 변환 (24시간 형식에서 12시간 형식으로 변환) + const formattedHours = hours % 12 || 12; + + // 시간대와 시간을 문자열로 결합 + return `${meridiem} ${formattedHours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; +} + + document.addEventListener('DOMContentLoaded', function () { connect(); });