首页 > 解决方案 > @SubscribeMapping 方法在客户端订阅时不会被命中

问题描述

我的 Spring Boot 应用程序中有以下 websocket 配置 + 端点:

控制器:

@Controller
public class QuoteController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @SubscribeMapping("/quote")
    public void singleQuote() {
        System.out.println("Salam");
    }
}

和配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    public static final String WEBSOCKET_HANDSHAKE_ENDPOINT_URI = "/api/wsocket";
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic"); /*Enable a simple in-memory broker for the clients to subscribe to channels and receive messages*/
        config.setApplicationDestinationPrefixes("/ws"); /*The prefix for the endpoints in the controller*/
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        /*websocket handshaking endpoint*/
        registry.addEndpoint(WEBSOCKET_HANDSHAKE_ENDPOINT_URI)
            /*TODO remove this after development*/
            .setAllowedOrigins("http://localhost:8080");
    }

}

以及基于 Web 的 js 客户端:

this.client = new Client();
this.client.configure({
  brokerURL: `ws://localhost:8553/api/wsocket`,
  onConnect: () => {
    console.log('onConnect');
    this.client.subscribe('/topic/quote', message => {
      console.log(message);
      debugger;
    });
  },
  // Helps during debugging, remove in production
  debug: (str) => {
    console.log(new Date(), str);
  }
});
this.client.activate();

我预计singleQuote会被击中,但事实并非如此。为什么?SubscribeMapping当客户端订阅频道时,不应该调用带有注释的方法吗?

标签: javaspring

解决方案


推荐阅读