【问题标题】:Spring Websocket with SockJs switch from XHR streaming to Websocket带有 SockJs 的 Spring Websocket 从 XHR 流式切换到 Websocket
【发布时间】:2017-05-13 19:10:48
【问题描述】:

我在客户端通过 STOMP 使用 Spring Websocket 和 SockJs (v1.1.1)。我使用下面的代码创建客户端。

SockJS = require('sockjs-client')
Stomp = require('webstomp-client')
stompClient = Stomp.over(new SockJS(url))

它工作正常。但它默认使用xhr_streaming 作为传输,这对我来说是不可取的。我想把它切换到websocket tranport。

stompClient = Stomp.over(new SockJS(url, null, {transports:'websocket'}))

但是这种方法对我不起作用。它与事件有关:

code: 2000
reason: "All transports failed"

我的 Spring Websocket 配置很简单:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {

    @Override
    public void configureStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/myEndpoint").setAllowedOrigins("*")
            .withSockJS();
    }

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

}

xhr_streaming 面临的主要问题是每个流请求都会更新用户会话的最后访问时间(我也使用 Spring Session)。但这不是我的应用程序的理想行为。那么我该如何解决呢? websocket 运输会有帮助吗?提前致谢。

【问题讨论】:

    标签: spring websocket stomp spring-websocket sockjs


    【解决方案1】:

    所以,我找到了原因。我正在使用带有 proxy 选项的 webpack 开发服务器。默认情况下,它不代理ws 协议请求(它会丢失标头)。您需要将ws: true 添加到您的配置中,一切都会正常工作。

    proxy: {
            '/api/*': {
                target: 'http://localhost:8080',
                pathRewrite: {'^/api' : ''},
                ws: true 
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-07-04
      • 2015-07-24
      • 2017-10-04
      • 2021-08-27
      • 2015-01-25
      • 2018-04-05
      • 2019-08-07
      • 2017-05-29
      • 2016-05-02
      相关资源
      最近更新 更多