-
Notifications
You must be signed in to change notification settings - Fork 16
[문은서] Sprint12 #169
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
base: 문은서
Are you sure you want to change the base?
[문은서] Sprint12 #169
Conversation
| import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
|
||
| @Configuration | ||
| @EnableWebSocketMessageBroker |
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.
WebSocket 클래스 설정
| @Controller | ||
| @RequiredArgsConstructor | ||
| public class MessageWebSocketController { | ||
|
|
||
| private final SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| @MessageMapping("/messages") | ||
| public void sendMessage(MessageCreateRequest request) { | ||
| String destination = "/sub/channels/" + request.channelId() + ".messages"; | ||
| messagingTemplate.convertAndSend(destination, request.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.
메세지 송신 처리
| @Component | ||
| @RequiredArgsConstructor | ||
| public class WebSocketRequiredEventListener { | ||
| private final SimpMessagingTemplate simpMessagingTemplate; | ||
|
|
||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| public void handleWebSocketMessage(MessageCreatedEvent event) { | ||
| String destination = "/sub/channels/" + event.channelId() + ".messages"; | ||
| simpMessagingTemplate.convertAndSend(destination, event.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.
메세지 수신 처리
| eventPublisher.publishEvent( | ||
| new MessageCreatedEvent(message.getId(), channelId, author.getId(), message.getContent()) |
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.
이벤트 발행
| private final ConcurrentLinkedDeque<UUID> eventIdQueue = new ConcurrentLinkedDeque<>(); | ||
| // 실제 이벤트 데이터를 저장(eventId -> message) | ||
| private final Map<UUID, SseMessage> messages = new ConcurrentHashMap<>(); |
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.
이벤트 순서 보장 및 동시성 보장
| @Service | ||
| @RequiredArgsConstructor | ||
| public class SseService { | ||
| private final SseEmitterRepository sseEmitterRepository; | ||
| private final SseMessageRepository sseMessageRepository; |
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.
SSE 관련 로직 처리
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.
docker-compose 설정 완료
| // Ping 테스트 후 끊긴 연결을 제거 | ||
| @Scheduled(fixedDelay = 1000 * 60 * 30) | ||
| public void cleanUp() { | ||
| for (UUID receiverId : sseEmitterRepository.findAllIds()) { | ||
| List<SseEmitter> emitters = sseEmitterRepository.findAllById(receiverId); | ||
| List<SseEmitter> deadEmitters = new ArrayList<>(); | ||
|
|
||
| for (SseEmitter emitter : emitters) { | ||
| boolean alive = ping(emitter); | ||
| if (!alive) { | ||
| deadEmitters.add(emitter); | ||
| } | ||
| } | ||
| for (SseEmitter emitter : deadEmitters) { | ||
| sseEmitterRepository.remove(receiverId, emitter); | ||
| } | ||
| } | ||
| } |
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.
주기적으로 응답 없는 연결 제외
| public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
|
||
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
| // 메세지 구독 경로 prefix | ||
| registry.enableSimpleBroker("/sub"); | ||
|
|
||
| // 메세지 발행 경로 prefix | ||
| registry.setApplicationDestinationPrefixes("/pub"); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
| registry.addEndpoint("/ws") | ||
| .setAllowedOrigins("*") | ||
| .withSockJS(); | ||
| } | ||
| } |
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.
WebSocketMessageBrokerConfigurer 통해 HTTP 요청 간 JWT 인증 수행
요구사항
기본
웹소켓 구현하기
SSE 구현하기
SSE 환경을 구성하세요.
connect: SseEmitter 객체를 생성합니다.
send, broadcast: SseEmitter 객체를 통해 이벤트를 전송합니다.
cleanUp: 주기적으로 ping을 보내서 만료된 SseEmitter 객체를 삭제합니다.
ping: 최초 연결 또는 만료 여부를 확인하기 위한 용도로 더미 이벤트를 보냅니다.
각 메시지 별로 고유한 ID를 부여합니다.
클라이언트에서 LastEventId를 전송해 이벤트 유실 복원이 가능하도록 해야 합니다.
기존에 클라이언트에서 폴링 방식으로 주기적으로 요청하던 데이터를 SSE를 이용해 서버에서 실시간으로 전달하는 방식으로 리팩토링하세요.
새 알림이 생성되었을 때 클라이언트에 이벤트를 전송하세요.
클라이언트는 이 이벤트를 수신하면 알림 목록에 알림을 추가합니다.
이벤트 명세
배포 아키텍처 구성하기
심화
주요 변경사항
스크린샷
멘토에게