![](/img/trans.png)
[英]Websockets Using Spring + SockJS + STOMP on Magnolia CMS
[英]Spring with STOMP over SockJS and Tomcat not upgrading to Websockets
我正在使用带有SockJS的Websockets上的STOMP和使用JWT的Rest Endpoint来构建无状态Spring(4.2.4.RELEASE)解决方案,以使用全双工通信连接移动设备。 我将Tomcat 8.0.33用作Web服务器,并使用html和sockjs javascript客户端进行测试。 stomp协议可以使用http后备协议正常运行,但我不能仅使用websocket协议来实现。 我以多种方式尝试了CORS,但是我不确定这是Tomcat问题还是仅仅是错误的spring配置。 即使在相同的域和端口中,我也测试了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
{
}
}
从版本8开始,Tomcat中支持Websocket。 检查您的Tomcat版本并升级。
此外,请检查您的浏览器是否支持Websocket。
我解决了我的问题,实际上代码不错,但是防病毒软件(Kaspersky)打开后立即关闭了客户端浏览器上的连接。 它迫使SockJS退回到另一种策略。 我在关闭防病毒功能并且Websocket传输运行良好的情况下测试了客户端。 在Mac和Linux上也进行了测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.