我正在使用 Spring 5:如何检测SUBSCRIBE来自 Stomp 客户端的消息?根据我的理解,@SubscribeMapping应该让我的控制器方法在客户端订阅主题时被调用,但这并没有发生。这是我的服务器控制器:@Controllerpublic class MessageController { // ... @MessageMapping("/chat/{mId}") @SendTo("/topic/messages") public OutputMessage send(Message message, @DestinationVariable("mId") String mid, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception { // ... } @SuppressWarnings("unused") @SubscribeMapping({ "/", "/chat", "/topic/messages", "/messages", "/*" }) public void listen(Message message, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception { int i = 0; System.out.println("subscribed"); }}服务器配置:@Configuration@ComponentScan(basePackages= { "websockets" })@EnableWebSocketMessageBrokerpublic class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/chat"); registry.addEndpoint("/chat").withSockJS(); } @Override public void configureWebSocketTransport(WebSocketTransportRegistration registry) { WebSocketMessageBrokerConfigurer.super.configureWebSocketTransport(registry); }}
1 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
您还需要将“主题”注册为应用程序目标主题config.setApplicationDestinationPrefixes({"/app", "/topic"});
。
否则 Spring 不会将订阅消息转发到应用程序,而只是将其转发到消息代理通道。
添加回答
举报
0/150
提交
取消