-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat #95] 채팅 전송 웹소켓 API #96
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33c8087
[chore] : application.yml 환경별 분리
hyun2371 2c7f662
[chore] : 웹소켓 라이브러리 추가
hyun2371 4ed0152
[feat] : 웹소켓 설정 파일 추가
hyun2371 62d9ebb
[feat] : ws cors 허용
hyun2371 b0192ee
[feat] : chatMessage 엔티티 내 mediaUrl 필드 삭제
hyun2371 7e7dfcc
[test] : mediaUrl 필드 삭제 반영
hyun2371 b941949
[feat] : 메시지 전송 임시 로직 삭제
hyun2371 aa16e95
[feat] : 채팅 메시지 저장하는 비즈니스 로직 추가
hyun2371 16b6553
[feat] : 웹소켓 통신 로직 추가 - 인메모리 브로커 사용
hyun2371 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mediaUrl(사진, 동영상) 값을 content에 넣겠다는 말씀으로 이해했는데 맞을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵 맞습니다!