Search in sources :

Example 1 with SubProtocolWebSocketHandler

use of org.springframework.web.socket.messaging.SubProtocolWebSocketHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupport method stompWebSocketHandlerMapping.

@Bean
public HandlerMapping stompWebSocketHandlerMapping() {
    WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler());
    WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(handler, getTransportRegistration(), messageBrokerTaskScheduler());
    registry.setApplicationContext(getApplicationContext());
    registerStompEndpoints(registry);
    return registry.getHandlerMapping();
}
Also used : WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) Bean(org.springframework.context.annotation.Bean)

Example 2 with SubProtocolWebSocketHandler

use of org.springframework.web.socket.messaging.SubProtocolWebSocketHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupportTests method clientInboundChannelSendMessage.

@Test
void clientInboundChannelSendMessage() throws Exception {
    ApplicationContext context = createContext(TestChannelConfig.class, TestConfigurer.class);
    TestChannel channel = context.getBean("clientInboundChannel", TestChannel.class);
    SubProtocolWebSocketHandler webSocketHandler = context.getBean(SubProtocolWebSocketHandler.class);
    List<ChannelInterceptor> interceptors = channel.getInterceptors();
    assertThat(interceptors.get(interceptors.size() - 1).getClass()).isEqualTo(ImmutableMessageChannelInterceptor.class);
    TestWebSocketSession session = new TestWebSocketSession("s1");
    session.setOpen(true);
    webSocketHandler.afterConnectionEstablished(session);
    webSocketHandler.handleMessage(session, StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build());
    Message<?> message = channel.messages.get(0);
    StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    assertThat(accessor).isNotNull();
    assertThat(accessor.isMutable()).isFalse();
    assertThat(accessor.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    assertThat(accessor.getDestination()).isEqualTo("/foo");
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) TestWebSocketSession(org.springframework.web.socket.handler.TestWebSocketSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 3 with SubProtocolWebSocketHandler

use of org.springframework.web.socket.messaging.SubProtocolWebSocketHandler in project spring-framework by spring-projects.

the class SockJsWebSocketHandlerTests method getSubProtocols.

@Test
public void getSubProtocols() throws Exception {
    SubscribableChannel channel = mock(SubscribableChannel.class);
    SubProtocolWebSocketHandler handler = new SubProtocolWebSocketHandler(channel, channel);
    StompSubProtocolHandler stompHandler = new StompSubProtocolHandler();
    handler.addProtocolHandler(stompHandler);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    DefaultSockJsService service = new DefaultSockJsService(scheduler);
    WebSocketServerSockJsSession session = new WebSocketServerSockJsSession("1", service, handler, null);
    SockJsWebSocketHandler sockJsHandler = new SockJsWebSocketHandler(service, handler, session);
    assertThat(sockJsHandler.getSubProtocols()).isEqualTo(stompHandler.getSupportedProtocols());
}
Also used : WebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) StompSubProtocolHandler(org.springframework.web.socket.messaging.StompSubProtocolHandler) TaskScheduler(org.springframework.scheduling.TaskScheduler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.jupiter.api.Test)

Example 4 with SubProtocolWebSocketHandler

use of org.springframework.web.socket.messaging.SubProtocolWebSocketHandler in project spring-integration by spring-projects.

the class TestServerConfig method subProtocolWebSocketHandler.

@Bean
public WebSocketHandler subProtocolWebSocketHandler() {
    SubProtocolWebSocketHandler webSocketHandler = new SubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
    webSocketHandler.setDefaultProtocolHandler(stompSubProtocolHandler());
    return webSocketHandler;
}
Also used : SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) Bean(org.springframework.context.annotation.Bean)

Example 5 with SubProtocolWebSocketHandler

use of org.springframework.web.socket.messaging.SubProtocolWebSocketHandler in project spring-framework by spring-projects.

the class WebSocketMessageBrokerConfigurationSupport method stompWebSocketHandlerMapping.

@Bean
public HandlerMapping stompWebSocketHandlerMapping(WebSocketHandler subProtocolWebSocketHandler, TaskScheduler messageBrokerTaskScheduler) {
    WebSocketHandler handler = decorateWebSocketHandler(subProtocolWebSocketHandler);
    WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(handler, getTransportRegistration(), messageBrokerTaskScheduler);
    ApplicationContext applicationContext = getApplicationContext();
    if (applicationContext != null) {
        registry.setApplicationContext(applicationContext);
    }
    registerStompEndpoints(registry);
    return registry.getHandlerMapping();
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) WebSocketHandler(org.springframework.web.socket.WebSocketHandler) SubProtocolWebSocketHandler(org.springframework.web.socket.messaging.SubProtocolWebSocketHandler) Bean(org.springframework.context.annotation.Bean)

Aggregations

SubProtocolWebSocketHandler (org.springframework.web.socket.messaging.SubProtocolWebSocketHandler)9 Test (org.junit.jupiter.api.Test)4 ApplicationContext (org.springframework.context.ApplicationContext)3 Bean (org.springframework.context.annotation.Bean)3 TaskScheduler (org.springframework.scheduling.TaskScheduler)3 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)3 StompSubProtocolHandler (org.springframework.web.socket.messaging.StompSubProtocolHandler)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 TestWebSocketSession (org.springframework.web.socket.handler.TestWebSocketSession)2 SubProtocolHandler (org.springframework.web.socket.messaging.SubProtocolHandler)2 ArrayList (java.util.ArrayList)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)1 DefaultSubscriptionRegistry (org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry)1 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)1