我使用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");
中添加队列 - 订阅url,如
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)}
原因是url不正确,在服务器上,发送参数应该是这样的:
user: admin
url: /location(right) vs /queue/location(wrong)
在客户端上,订阅者应该像这样:
/user/admin/location or /user/{username}/location