Spring-boot: websocket fails in Spring-Boot 1.2.2, works in 1.2.1

Created on 2 Mar 2015  路  2Comments  路  Source: spring-projects/spring-boot

I have a simple test websocket server that echoes back messages from clients. In spring-boot 1.2.1, it works fine, but in 1.2.2, it now fails to work. It looks like the clients do not connect successfully.

This is the server warning message when a client attempts to connect:
WARN 5422 --- [nio-8080-exec-1] org.springframework.web.util.WebUtils : Failed to parse Origin header value [null]

@ComponentScan
@Controller
@EnableAutoConfiguration
@EnableWebSocket
public class EchoWebSocketServer implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/test");
    }

    @Bean
    public WebSocketHandler handler() {
        return new ExceptionWebSocketHandlerDecorator(new Handler() );
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(EchoWebSocketServer.class, args);
    }
}
public class Handler implements WebSocketHandler{
    @Override
    public void handleMessage(WebSocketSession session,
            WebSocketMessage<?> encodedMessage) throws Exception {
        if (encodedMessage instanceof org.springframework.web.socket.TextMessage) {
            org.springframework.web.socket.TextMessage castedTextMessage = (org.springframework.web.socket.TextMessage) encodedMessage;
            String message = castedTextMessage.getPayload();
            session.sendMessage(new org.springframework.web.socket.TextMessage(message));
            System.out.println(message);
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession arg0, CloseStatus arg1)
            throws Exception {
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession arg0)
            throws Exception {
        org.springframework.web.socket.TextMessage message = 
                new org.springframework.web.socket.TextMessage("connected");
    }

    @Override
    public void handleTransportError(WebSocketSession arg0, Throwable arg1)
            throws Exception {
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}

Most helpful comment

Indeed, that does appear to be the issue. Thanks.

The solution is straightforward:

registry.addHandler(handler(), "/test").setAllowedOrigins("*");

All 2 comments

This might be caused by Spring Framework 4.1.5's stricter handling of SockJS origins (@bclozel):
https://jira.spring.io/browse/SPR-12740
https://jira.spring.io/browse/SPR-12685

Indeed, that does appear to be the issue. Thanks.

The solution is straightforward:

registry.addHandler(handler(), "/test").setAllowedOrigins("*");
Was this page helpful?
0 / 5 - 0 ratings