java - @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
当客户端订阅频道时,不应该调用带有注释的方法吗?
解决方案
推荐阅读
- listview - 使用文本框中的按钮在 uwp 中填充列表视图
- spring - 使用 Spring Boot 下载大文件时出现 ClientAbortException
- c# - 将反序列化为接口的对象转换回其原始类型
- python - 在 Python 中计算矩阵的 3 范数
- python - 希望优化网格搜索以获得最佳参数
- python - 如果函数在不应该执行时执行
- c# - 在访问者模式中的节点之间传递数据
- javascript - 在我的代码中,mousemove 属性不起作用并更改了输入类型范围。能解释一下为什么吗?
- python - 使用 python 中的命令打开多个 selenium webdrivers(任务)
- python - VSCode忽略断点