-
Notifications
You must be signed in to change notification settings - Fork 0
feat(#17): 채팅 WS 기본 구성 및 테스트 추가 #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
187c316
feat(#17): 채팅 WS 기본 구성 추가
1000hyehyang 7cea434
test(#17): 채팅 WS 메시지 테스트 분리 및 케이스 추가
1000hyehyang 9fc7a76
feat(#17): 채팅 API 접근 허용 범위 추가
1000hyehyang eeb5d53
fix(#17): 하드코딩 되고 필요 없었던 CORS Origin 설정 정리
1000hyehyang fa3f894
fix(#17): ACK 상태 비교 Enum으로 통일
1000hyehyang 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 hidden or 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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/example/RealMatch/chat/presentation/config/ChatWebSocketConfig.java
This file contains hidden or 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,41 @@ | ||
| package com.example.RealMatch.chat.presentation.config; | ||
|
|
||
| import org.springframework.beans.factory.ObjectProvider; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.lang.NonNull; | ||
| 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; | ||
| import org.springframework.web.socket.server.HandshakeHandler; | ||
|
|
||
| @Configuration | ||
| @EnableWebSocketMessageBroker | ||
| public class ChatWebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
|
||
| @Value("${cors.allowed-origin}") | ||
| private String allowedOrigin; | ||
| private final ObjectProvider<HandshakeHandler> handshakeHandlerProvider; | ||
|
|
||
| public ChatWebSocketConfig(ObjectProvider<HandshakeHandler> handshakeHandlerProvider) { | ||
| this.handshakeHandlerProvider = handshakeHandlerProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public void registerStompEndpoints(@NonNull StompEndpointRegistry registry) { | ||
| var registration = registry.addEndpoint("/ws/chat") | ||
| .setAllowedOrigins(allowedOrigin); | ||
| HandshakeHandler handshakeHandler = handshakeHandlerProvider.getIfAvailable(); | ||
| if (handshakeHandler != null) { | ||
| registration.setHandshakeHandler(handshakeHandler); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void configureMessageBroker(@NonNull MessageBrokerRegistry registry) { | ||
| registry.setApplicationDestinationPrefixes("/app"); | ||
| registry.setUserDestinationPrefix("/user"); | ||
| registry.enableSimpleBroker("/topic", "/queue"); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/example/RealMatch/chat/presentation/controller/ChatSocketController.java
This file contains hidden or 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,34 @@ | ||
| package com.example.RealMatch.chat.presentation.controller; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import org.springframework.messaging.handler.annotation.MessageMapping; | ||
| import org.springframework.messaging.handler.annotation.Payload; | ||
| import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
| import org.springframework.messaging.simp.annotation.SendToUser; | ||
| import org.springframework.stereotype.Controller; | ||
|
|
||
| import com.example.RealMatch.chat.presentation.controller.fixture.ChatSocketFixtureFactory; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatMessageCreatedEvent; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageAck; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageCommand; | ||
|
|
||
| import jakarta.validation.Valid; | ||
|
|
||
| @Controller | ||
| public class ChatSocketController { | ||
|
|
||
| private final SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| public ChatSocketController(SimpMessagingTemplate messagingTemplate) { | ||
| this.messagingTemplate = messagingTemplate; | ||
| } | ||
|
|
||
| @MessageMapping("/chat.send") | ||
| @SendToUser("/queue/chat.ack") | ||
| public ChatSendMessageAck sendMessage(@Valid @Payload ChatSendMessageCommand command) { | ||
| ChatMessageCreatedEvent event = ChatSocketFixtureFactory.sampleMessageCreatedEvent(command); | ||
| messagingTemplate.convertAndSend("/topic/rooms/" + command.roomId(), Objects.requireNonNull(event)); | ||
| return ChatSocketFixtureFactory.sampleAck(command, event.message().messageId()); | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
.../com/example/RealMatch/chat/presentation/controller/fixture/ChatSocketFixtureFactory.java
This file contains hidden or 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,66 @@ | ||
| package com.example.RealMatch.chat.presentation.controller.fixture; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatAttachmentStatus; | ||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatAttachmentType; | ||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatMessageType; | ||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatSendMessageAckStatus; | ||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatSenderType; | ||
| import com.example.RealMatch.chat.presentation.dto.response.ChatAttachmentInfoResponse; | ||
| import com.example.RealMatch.chat.presentation.dto.response.ChatMessageResponse; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatMessageCreatedEvent; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageAck; | ||
| import com.example.RealMatch.chat.presentation.dto.websocket.ChatSendMessageCommand; | ||
|
|
||
| public final class ChatSocketFixtureFactory { | ||
1000hyehyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private ChatSocketFixtureFactory() { | ||
| } | ||
|
|
||
| public static ChatMessageCreatedEvent sampleMessageCreatedEvent(ChatSendMessageCommand command) { | ||
| ChatAttachmentInfoResponse attachment = sampleAttachment(command); | ||
| String content = command.messageType() == ChatMessageType.TEXT ? command.content() : null; | ||
|
|
||
| ChatMessageResponse message = new ChatMessageResponse( | ||
| 8001L, | ||
| command.roomId(), | ||
| 202L, | ||
| ChatSenderType.USER, | ||
| command.messageType(), | ||
| content, | ||
| attachment, | ||
| null, | ||
| LocalDateTime.of(2025, 1, 1, 10, 2), | ||
| command.clientMessageId() | ||
| ); | ||
| return new ChatMessageCreatedEvent(command.roomId(), message); | ||
| } | ||
|
|
||
| public static ChatSendMessageAck sampleAck(ChatSendMessageCommand command, Long messageId) { | ||
| return new ChatSendMessageAck( | ||
| command.clientMessageId(), | ||
| messageId, | ||
| ChatSendMessageAckStatus.SUCCESS | ||
| ); | ||
| } | ||
|
|
||
| private static ChatAttachmentInfoResponse sampleAttachment(ChatSendMessageCommand command) { | ||
| ChatMessageType messageType = command.messageType(); | ||
| if (messageType != ChatMessageType.IMAGE && messageType != ChatMessageType.FILE) { | ||
| return null; | ||
| } | ||
| ChatAttachmentType attachmentType = messageType == ChatMessageType.IMAGE | ||
| ? ChatAttachmentType.IMAGE | ||
| : ChatAttachmentType.FILE; | ||
| return new ChatAttachmentInfoResponse( | ||
| command.attachmentId(), | ||
| attachmentType, | ||
| messageType == ChatMessageType.IMAGE ? "image/png" : "application/octet-stream", | ||
| messageType == ChatMessageType.IMAGE ? "photo.png" : "file.bin", | ||
| 204800L, | ||
| "https://cdn.example.com/attachments/8001", | ||
| ChatAttachmentStatus.READY | ||
| ); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
...main/java/com/example/RealMatch/chat/presentation/dto/enums/ChatSendMessageAckStatus.java
This file contains hidden or 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,6 @@ | ||
| package com.example.RealMatch.chat.presentation.dto.enums; | ||
|
|
||
| public enum ChatSendMessageAckStatus { | ||
| SUCCESS, | ||
| FAILED | ||
| } |
9 changes: 9 additions & 0 deletions
9
...n/java/com/example/RealMatch/chat/presentation/dto/websocket/ChatMessageCreatedEvent.java
This file contains hidden or 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,9 @@ | ||
| package com.example.RealMatch.chat.presentation.dto.websocket; | ||
|
|
||
| import com.example.RealMatch.chat.presentation.dto.response.ChatMessageResponse; | ||
|
|
||
| public record ChatMessageCreatedEvent( | ||
| Long roomId, | ||
| ChatMessageResponse message | ||
| ) { | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/RealMatch/chat/presentation/dto/websocket/ChatSendMessageAck.java
This file contains hidden or 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,10 @@ | ||
| package com.example.RealMatch.chat.presentation.dto.websocket; | ||
|
|
||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatSendMessageAckStatus; | ||
|
|
||
| public record ChatSendMessageAck( | ||
| String clientMessageId, | ||
| Long messageId, | ||
| ChatSendMessageAckStatus status | ||
| ) { | ||
| } |
14 changes: 14 additions & 0 deletions
14
...in/java/com/example/RealMatch/chat/presentation/dto/websocket/ChatSendMessageCommand.java
This file contains hidden or 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,14 @@ | ||
| package com.example.RealMatch.chat.presentation.dto.websocket; | ||
|
|
||
| import com.example.RealMatch.chat.presentation.dto.enums.ChatMessageType; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ChatSendMessageCommand( | ||
| @NotNull Long roomId, | ||
| @NotNull ChatMessageType messageType, | ||
| String content, | ||
| Long attachmentId, | ||
| @NotNull String clientMessageId | ||
| ) { | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.