오늘의 공부
STOMP 요약정리
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp/chat")
.setAllowedOrigins("http://*:8080", "http://*.*.*.*:8080")
.setAllowedOriginPatterns("http://*:63342", "http://*.*.*.*:63342")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/pub");
registry.enableSimpleBroker("/sub");
}
@MessageMapping(value = "/chat/message")
public void message(ChatRequestDto message){
ChatResponseDto savedMessage = chatService.saveMessage(message);
template.convertAndSend("/sub/chat/group/" + savedMessage.getGroupId(), savedMessage);
}
- addEndpoint : 소켓 생성을 위한 경로
- setAllowedOrigins : 소켓 통신을 위해 열려있어야 하는 경로
- 클라이언트 입장
- pub로 보낸다
- pub로 보내는 메세지 안에 어디로 보낼지 도착지 정보도 들어있다.
- 서로 다른 주소가 적힌 편지가 하나의 우체통에 모이듯
- sub로 받는다
- sub/chat/group/{groupId} 경로를 구독한다
- 서버 입장
- pub로 데이터가 들어온다
- sub로 데이터를 보낸다
- 데이터 안에 들어있던 도착지 정보를 꺼내와서 해당 위치로 데이터를 뿌린다
- 이 과정에서 사용되는 것이 Message Broker
- 스프링의 기본 브로커인 SimpleBroker를 사용해도 되지만, 스프링의 메모리를 사용하므로 메세지가 많아질 경우 부담이 될 수 있다.
- SimpleBroker를 사용하지 않고 외부에 Message Broker를 두고 사용할 수 있다
문제와 시도
socket info not found
GET http://localhost:63342/stomp/chat/info?t=1678038167739 404 (Not Found)
해결과 학습
socket info not found
- StompWebSocketConfig에
@EnableWebSocketMessageBroker 어노테이션 달려있는지 확인
- StompWebSocketConfig의 registerStompEndpoints에
.withSockJS() 적용되어있는지 확인
- StompWebSocketConfig의 registerStompEndpoints에
.setAllowedOrigins() 올바르게 허용되어있는지 확인
- 위 3가지 과정을 거쳐가며 확인하였으나 문제가 발견되지 않았다.
- 이후 프론트쪽 자바스크립트에서 소켓 생성 포트가 잘못 적용된것을 확인하고 수정하였다.
var sockJs = new SockJS("http://localhost:8080/stomp/chat");
메모