gpt4 book ai didi

java - 在 SockJS 和 Tomcat 上使用 STOMP 的 Spring 没有升级到 Websockets

转载 作者:行者123 更新时间:2023-11-28 23:26:31 24 4
gpt4 key购买 nike

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

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
{

}
}

最佳答案

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

关于java - 在 SockJS 和 Tomcat 上使用 STOMP 的 Spring 没有升级到 Websockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com