最近在做一个WebSocket项目,后台使用了Stomp协议的WebSocketConfigurer,但是前端在测试的时候,发现一个很奇怪的问题:
本地的测试代码,最初我使用了Java+SockJS实现:
@Configuration @EnableWebSocketMessageBroker public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/density") .setAllowedOrigins("*") //解决跨域问题 .withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); } }