【问题标题】:Spring with STOMP over SockJS and Tomcat not upgrading to WebsocketsSpring with STOMP over SockJS 和 Tomcat 未升级到 Websockets
【发布时间】:2016-07-26 17:13:47
【问题描述】:

我正在构建一个无状态 Spring (4.2.4.RELEASE) 解决方案,使用 STOMP over Websockets 和 SockJS 和一个使用 JWT 的 Rest Endpoint 来连接具有全双工通信的移动设备。我使用 Tomcat 8.0.33 作为 Web 服务器并使用带有 sockjs javascript 客户端的 html 进行测试。 stomp 协议使用 http 后备可以正常工作,但我不能只使用 websocket 协议。我以多种方式尝试了 CORS,但我不确定这是 Tomcat 问题还是弹簧配置不好。即使在相同的域和端口中,我也测试了我的 html,而 SockJS 仍然退回到 xhr 或 iframe。

WebScoketConfig.java

@EnableWebSocketMessageBroker
 public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer
{

@Override
public void registerStompEndpoints(StompEndpointRegistry registry)
{
    RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
    registry.addEndpoint("/ws").setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
    .setAllowedOrigins("*").withSockJS().setSessionCookieNeeded(false)
    .setStreamBytesLimit(512 * 1024)
    .setHttpMessageCacheSize(1000)
    .setDisconnectDelay(30 * 1000);
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
    registration.taskExecutor().corePoolSize(50);
}


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

 public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.setMessageSizeLimit(500 * 1024);
    registration.setSendBufferSizeLimit(1024 * 1024);
    registration.setSendTimeLimit(20000);
 }
}

WebSecurityConfig.java

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter
    {

        @Override
        protected void configure(HttpSecurity http) throws Exception
        {
            http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/**").permitAll();                
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception
        {

        }
    }

【问题讨论】:

    标签: java spring tomcat spring-websocket sockjs


    【解决方案1】:

    我解决了我的问题,实际上代码很好,但是杀毒软件(卡巴斯基)在打开我的客户端浏览器后就关闭了连接。它迫使 SockJS 退回到不同的策略。我在关闭防病毒软件的情况下测试了客户端,并且 Websocket 传输运行良好。也在 mac 和 linux 上测试过。

    【讨论】:

      【解决方案2】:

      Tomcat 从版本 8 开始支持 Websocket。检查您的 Tomcat 版本并升级。

      进一步检查您的浏览器是否支持 Websocket。

      【讨论】:

      • 我使用的是最新的 Tomcat 版本。它支持 websockets。
      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2016-12-29
      • 2014-03-17
      • 2017-08-23
      • 2017-01-06
      • 2019-12-31
      • 2015-12-16
      • 2021-06-04
      相关资源
      最近更新 更多