-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [chore] : application.yml 환경별 분리 * [chore] : 웹소켓 라이브러리 추가 * [feat] : 웹소켓 설정 파일 추가 * [feat] : ws cors 허용 * [feat] : chatMessage 엔티티 내 mediaUrl 필드 삭제 * [test] : mediaUrl 필드 삭제 반영 * [feat] : 메시지 전송 임시 로직 삭제 * [feat] : 채팅 메시지 저장하는 비즈니스 로직 추가 * [feat] : 웹소켓 통신 로직 추가 - 인메모리 브로커 사용
Showing
14 changed files
with
101 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/dnd/gongmuin/chat/controller/ChatMessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.dnd.gongmuin.chat.controller; | ||
|
||
import org.springframework.messaging.handler.annotation.DestinationVariable; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.messaging.handler.annotation.SendTo; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import com.dnd.gongmuin.chat.dto.ChatMessageRequest; | ||
import com.dnd.gongmuin.chat.dto.ChatMessageResponse; | ||
import com.dnd.gongmuin.chat.service.ChatMessageService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class ChatMessageController { | ||
|
||
private final ChatMessageService chatMessageService; | ||
|
||
@MessageMapping("/chat-rooms/{chatRoomId}") | ||
@SendTo("/sub/chat-rooms/{chatRoomId}") | ||
public ChatMessageResponse sendMessage( | ||
@DestinationVariable("chatRoomId") Long chatRoomId, | ||
@Payload ChatMessageRequest request | ||
) { | ||
return chatMessageService.saveChatMessage(request, chatRoomId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
src/main/java/com/dnd/gongmuin/chat/dto/ChatMessageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package com.dnd.gongmuin.chat.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record ChatMessageRequest( | ||
@NotBlank(message = "채팅 내용을 입력해주세요.") | ||
String content, | ||
@NotBlank(message = "채팅 타입을 입력해주세요.") | ||
String type, | ||
String mediaUrl | ||
|
||
@NotNull(message = "회원 아이디를 입력해주세요.") | ||
Long memberId | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dnd/gongmuin/chat/service/ChatMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.dnd.gongmuin.chat.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.dnd.gongmuin.chat.domain.ChatMessage; | ||
import com.dnd.gongmuin.chat.dto.ChatMapper; | ||
import com.dnd.gongmuin.chat.dto.ChatMessageRequest; | ||
import com.dnd.gongmuin.chat.dto.ChatMessageResponse; | ||
import com.dnd.gongmuin.chat.repository.ChatMessageRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ChatMessageService { | ||
|
||
private final ChatMessageRepository chatMessageRepository; | ||
|
||
@Transactional | ||
public ChatMessageResponse saveChatMessage( | ||
ChatMessageRequest request, | ||
Long chatRoomId | ||
) { | ||
Long memberId = request.memberId(); | ||
ChatMessage chatMessage = chatMessageRepository.save(ChatMapper.toChatMessage(request, chatRoomId, memberId)); | ||
log.info("chatRoomId = {}, memberId= {}, chatMessageId= {}", chatRoomId, memberId, chatMessage.getId()); | ||
return ChatMapper.toChatMessageResponse(chatMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dnd/gongmuin/common/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.dnd.gongmuin.common.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
registry.enableSimpleBroker("/sub"); // 구독 경로 | ||
registry.setApplicationDestinationPrefixes("/pub"); // 발행 경로 | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/ws") // 웹소켓 연결 엔드포인트 | ||
.setAllowedOriginPatterns("*") | ||
.withSockJS(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ public static ChatMessage chatMessage() { | |
"하하", | ||
1L, | ||
1L, | ||
null, | ||
MessageType.TEXT | ||
); | ||
} | ||
|