在客户端的JavaScript中我有:
stomp.subscribe("/topic/path", function (message) {
console.info("message received");
});
并且在服务器端执行
public class Controller {
private final MessageSendingOperations<String> messagingTemplate;
@Autowired
public Controller(MessageSendingOperations<String> messagingTemplate) {
this.messagingTemplate = messagingTemplate;
}
@SubscribeMapping("/topic/path")
public void subscribe() {
LOGGER.info("before send");
messagingTemplate.convertAndSend(/topic/path, "msg");
}
}
从这个设置中,我偶尔会遇到消息丢失的情况(大约每30次页面刷新一次),这意味着我在客户端看不到“消息已收到”的消息,也看不到来自Chrome调试工具的WebSocket流量。
无论何时,在服务器端都会记录“发送前”。
看起来像是当我在subscribe()方法中调用MessageSendingOperations时它还没有准备好。(如果我在调用messagingTemplate.convertAndSend之前加上Thread.sleep(50);,问题就会消失(或者很少出现))
我想知道是否有人之前也遇到过这种情况,并且是否有事件可以告诉我MessageSendingOperations是否准备好了。