Loading...
Spring Framework Reference Documentation 7.0.2의 Enable STOMP의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
STOMP over WebSocket 지원은 spring-messaging 및
spring-websocket module에서 사용할 수 있습니다. 해당 의존성을 가지면, 다음 예제에서 보듯이 WebSocket을 통해 STOMP endpoint를 노출할 수 있습니다:
1@Configuration 2@EnableWebSocketMessageBroker 3public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer { 4 5 @Override 6 public void registerStompEndpoints(StompEndpointRegistry registry) { 7 // /portfolio is the HTTP URL for the endpoint to which a WebSocket (or SockJS) 8 // client needs to connect for the WebSocket handshake 9 registry.addEndpoint("/portfolio"); 10 } 11 12 @Override 13 public void configureMessageBroker(MessageBrokerRegistry config) { 14 // STOMP messages whose destination header begins with /app are routed to 15 // @MessageMapping methods in @Controller classes 16 config.setApplicationDestinationPrefixes("/app"); 17 // Use the built-in message broker for subscriptions and broadcasting and 18 // route messages whose destination header begins with /topic or /queue to the broker 19 config.enableSimpleBroker("/topic", "/queue"); 20 } 21}
1@Configuration 2@EnableWebSocketMessageBroker 3class WebSocketConfiguration : WebSocketMessageBrokerConfigurer { 4 5 override fun registerStompEndpoints(registry: StompEndpointRegistry) { 6 // /portfolio is the HTTP URL for the endpoint to which a WebSocket (or SockJS) 7 // client needs to connect for the WebSocket handshake 8 registry.addEndpoint("/portfolio") 9 } 10 11 override fun configureMessageBroker(config: MessageBrokerRegistry) { 12 // STOMP messages whose destination header begins with /app are routed to 13 // @MessageMapping methods in @Controller classes 14 config.setApplicationDestinationPrefixes("/app") 15 // Use the built-in message broker for subscriptions and broadcasting and 16 // route messages whose destination header begins with /topic or /queue to the broker 17 config.enableSimpleBroker("/topic", "/queue") 18 } 19}
1<beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:websocket="http://www.springframework.org/schema/websocket" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/websocket 8 https://www.springframework.org/schema/websocket/spring-websocket.xsd"> 9 10 <websocket:message-broker application-destination-prefix="/app"> 11 <websocket:stomp-endpoint path="/portfolio" /> 12 <websocket:simple-broker prefix="/topic, /queue"/> 13 </websocket:message-broker> 14 15</beans>
built-in simple broker의 경우,
/topic및/queueprefix는 어떠한 특별한<br>의미도 가지지 않습니다. 이들은 단지 pub-sub와 point-to-point<br>messaging(즉, 다수의 subscriber 대 하나의 consumer)을 구분하기 위한 관례일 뿐입니다. external broker를 사용할 때에는,<br>해당 broker의 STOMP page를 확인하여 어떤 종류의 STOMP destination과<br>prefix를 지원하는지 이해해야 합니다.
browser에서 연결하기 위해, STOMP의 경우 가장 활발하게 유지·관리되는 JavaScript 라이브러리인
stomp-js/stompjs를 사용할 수 있습니다.
다음 예제 코드는 이를 기반으로 합니다:
1const stompClient = new StompJs.Client({ 2 brokerURL: 'ws://domain.com/portfolio', 3 onConnect: () => { 4 // ... 5 } 6});
또는, SockJS를 통해 연결하는 경우, 서버 사이드에서
registry.addEndpoint("/portfolio").withSockJS()로
SockJS Fallback을 활성화하고,
JavaScript side에서는
those instructions을 따르면 됩니다.
앞선 예제의 stompClient는 login
및 passcode header를 지정할 필요가 없습니다. 설령 지정하더라도, 서버 사이드에서 무시되거나(정확히는<br>override되거나) 합니다. 인증에 대한 더 많은 정보는 Connecting to a Broker
및 Authentication을 참고하십시오.
더 많은 예제 코드는 다음을 참고하십시오:
Benefits
WebSocket Transport