Spring 4.1.1 WebSocket Stomp Subprotocol错误

时间:2014-10-31 15:15:43

标签: spring stomp spring-websocket

我们只是尝试将我们的一个项目从Spring 4.0.7.RELEASE迁移到4.1.1.RELEASE。 之后我们得到以下错误。

java.lang.IllegalStateException: Multiple protocol handlers configured and no protocol was negotiated. Consider configuring a default SubProtocolHandler.
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.findProtocolHandler(SubProtocolWebSocketHandler.java:294)
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.clearSession(SubProtocolWebSocketHandler.java:433)
at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.afterConnectionClosed(SubProtocolWebSocketHandler.java:423)
at org.springframework.web.socket.handler.WebSocketHandlerDecorator.afterConnectionClosed(WebSocketHandlerDecorator.java:85)
at org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator.afterConnectionClosed(LoggingWebSocketHandlerDecorator.java:71)
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.afterConnectionClosed(ExceptionWebSocketHandlerDecorator.java:91)
at org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession.close(AbstractSockJsSession.java:291)
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.tryCloseWithError(ExceptionWebSocketHandlerDecorator.java:60)
at org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator.afterConnectionEstablished(ExceptionWebSocketHandlerDecorator.java:50)
at org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession.delegateConnectionEstablished(AbstractSockJsSession.java:207)
at org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession.initializeDelegateSession(WebSocketServerSockJsSession.java:159)
at org.springframework.web.socket.sockjs.transport.handler.SockJsWebSocketHandler.afterConnectionEstablished(SockJsWebSocketHandler.java:87)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onOpen(StandardWebSocketHandlerAdapter.java:101)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:129)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:633)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1721)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我们没有更改配置上的任何内容(并且找不到任何我们必须更改任何内容的提示)。

以下是我们的WebSockets的服务器端配置。 (事实上​​它与Spring文档中的相同)。

@Configuration
@EnableWebSocketMessageBroker

public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
    registry.addEndpoint("/tyresearch").withSockJS();
    registry.addEndpoint("/tyresearch/changeConfiguration").withSockJS();
    registry.addEndpoint("/tyresearch/detailView").withSockJS();
}

@Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}
}

此外,对于以下方法之前描述的每个端点,我们都有:

@MessageMapping("/tyresearch")
public void startTyreSearch(@Payload final TyreSearchRequestContainerDto message, final Principal user, final StompHeaderAccessor) {
    ....
}

来自JavaScript的客户端调用类似于以下内容:

var socket = new $wnd.SockJS('/server/api/rest/tyresearch');
    var stompClient = $wnd.Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        console.log('Connected: ' + frame);
    });

有人有想法,有什么改变让我们在这里遇到麻烦以及如何解决这个问题?

非常感谢。

1 个答案:

答案 0 :(得分:1)

首先是bug。谢谢你指出来了!

我们提出了一个问题(https://jira.spring.io/browse/SPR-12403),它将在下周修复。

在此之前,你不能只用单一的端点来克服stomp端点的malty映射吗?

<强>更新

  

我收到一个错误,告诉我协议stomp 1.0,1.1和1.2已经映射

因为我们在这里确实存在问题,你需要以某种方式克服它,我建议你尝试覆盖bean:

@Bean
    public WebSocketHandler subProtocolWebSocketHandler() {
        return new SubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
    }

使用自定义实现,其中addProtocolHandler将有一个空主体,因为您说您已将STOMP定义为默认值。拥有该框架将无法添加更多SubProtocolHandler s。