Spring WebSocket ConvertAndSendToUser 不工作但 convertAndSend 工作 [英] Spring WebSocket ConvertAndSendToUser not working but convertAndSend working
问题描述
我是 Spring Boot websocket 和消息传递语义的新手.目前我可以使用以下代码发送私人消息.
I am new to Spring boot websocket and messaging semantics. Currently i am able to send private messages using the below code.
String queueName = "/user/" + username + "/queue/wishes";
simpMessagingTemplate.convertAndSend(queueName, message);
尝试使用 convertAndSendToUser 时,我没有收到任何错误,但没有发送消息.我知道使用 sendToUser 应该对目的地名称的形成方式略有变化,但我没有做对.
When trying to use convertAndSendToUser I am not getting any error but the message is not getting sent. I knew that with sendToUser there should be a slight change in how the destination name should be formed but I am not getting it right.
String queueName = "/user/queue/wishes";
simpMessagingTemplate.convertAndSendToUser(username, queueName, message);
下面是我的订阅代码.
stompClient.subscribe('/user/queue/wishes', function(message) {
alert(message);
})
推荐答案
终于解决了问题.我犯了一个愚蠢的错误.我正在填充 User DestinationPrefix 作为 WebSocket 配置的一部分.但没有为注入的 bean SimpMessaging 模板设置它.
Finally figured out the problem. I did a silly mistake. I was filling the User DestinationPrefix as part of WebSocket config. but didn't set it up for the injected bean SimpMessaging template.
这篇关于Spring WebSocket ConvertAndSendToUser 不工作但 convertAndSend 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!