当使用此方法向客户端广播消息时,我正在使用websocket向客户端发送消息。
private void pushInfoImpl(String url, String content) {
if (simpMessagingTemplate != null) {
simpMessagingTemplate.convertAndSend(url, content);
}
}
客户端可以成功收到消息。使用此方法发送指定用户时:
//send url:/queue/location
//user:admin
private void pushInfoImpl(String user, String url, String content) {
if (simpMessagingTemplate != null) {
simpMessagingTemplate.convertAndSendToUser(user, url, content);
}
}
客户端无法接收已发送的消息。我尝试过:
config.enableSimpleBroker("/topic","/queue");
stompClient.subscribe('/admin/queue/location', function (greeting) { console.log(greeting.body); });
.admin是用户。我该怎么做才能解决这个问题?使客户端成功收到服务器消息。这是日志:
15:03:28.806]-[Timer-5699]-[org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler]-{Processing MESSAGE destination=/queue/location-userdeqnkwuw session=null payload={"data":[{"status":2,"vehicleId":"cae21196-cb66-4256-88a6-7cdfb23e2c78"}],"desc"...(truncated)}
15:03:28.806]-[Timer-5701]-[org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler]-{Processing MESSAGE destination=/queue/location-userdeqnkwuw session=null payload={"data":[{"status":2,"vehicleId":"cae21196-cb66-4256-88a6-7cdfb23e2c78"}],"desc"...(truncated)}
15:03:28.806]-[Timer-5699]-[org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler]-{Processing MESSAGE destination=/queue/location-userdeqnkwuw session=null payload={"data":[{"status":2,"vehicleId":"cae21196-cb66-4256-88a6-7cdfb23e2c78"}],"desc"...(truncated)}
答案 0 :(得分:0)
原因是url不对,在服务器上,发送参数应该是这样的:
user: admin
url: /location(right) vs /queue/location(wrong)
在客户端,subribe应该是这样的:
/user/admin/location or /user/{username}/location