簡體   English   中英

WebSocket convertAndSendToUser無法接收消息

[英]WebSocket convertAndSendToUser can not received message

我使用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);
    }
}

客戶端無法接收已發送的消息。 我嘗試過的:

  1. config.enableSimpleBroker("/topic","/queue");添加隊列config.enableSimpleBroker("/topic","/queue");
  2. 訂閱網址如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)

在客戶端,subribe應該是這樣的:

/user/admin/location or /user/{username}/location

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM