Search code examples
tomcatwebsocketspring-websocket

Does spring-websocket's RestTemplateXhrTransport work?


I am trying to use the spring-websocket RestTemplateXhrTransport for an integration-test, but am unable to get it to work.

The server is setup with spring-websocket (4.1-RELEASE) with a by-the-book-configuration:

@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/socket").withSockJS().setClientLibraryUrl("../common/lib/sockjs.js");
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic/", "/user/");
        registry.setApplicationDestinationPrefixes("/app", "/topic");
    }
}

I would have expected the following code to work, but it just times out waiting for the preamble. If I look at the network traffic, I can see that the preamble is sent by the server, but the client just hangs waiting for data after having read the headers:

    List<Transport> transports = new ArrayList<>();
    RestTemplate restTemplate = new RestTemplate();
    RestTemplateXhrTransport xhrTransport = new RestTemplateXhrTransport(restTemplate);

    // This makes it work, but that is xhr-polling, and I want xhr-streaming.
    //xhrTransport.setXhrStreamingDisabled(true);

    transports.add(xhrTransport);
    SockJsClient sockJsClient = new SockJsClient(transports);
    AbstractWebSocketHandler webSocketHandler = new AbstractWebSocketHandler() {
        @Override
        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
            System.out.println("connected");
        }
    };
    sockJsClient.doHandshake(webSocketHandler, new WebSocketHttpHeaders(), new URI("http://localhost:8080/socket"))
            .get(3000, TimeUnit.SECONDS);

I am using a Tomcat application server. It fails to work with both 7.0.55 and 8.0.12.

I have also tried to look at IntegrationPortfolioTests from the spring-websockets-portfolio sample. If I comment out the line in the setup-method that adds WebSocketTransport as the primary transport and leave the line that uses RestTemplateXhrTransport in, this testcase does not work either.


Solution

  • Turns out that it was caused by my virusscanner's "online shield". As soon as I turned off AVG AntiVirus Business Editions' online shield, the transport works perfectly. Looks like it does not like streaming data.