WebSocket convertAndSendToUser无法接收消息

时间:2016-10-25 06:56:10

标签: java websocket

当使用此方法向客户端广播消息时,我正在使用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");
  2. 中添加队列
  3. 订阅网址,例如stompClient.subscribe('/admin/queue/location', function (greeting) { console.log(greeting.body); }); .admin是用户。
  4. 我该怎么做才能解决这个问题?使客户端成功收到服务器消息。这是日志:

    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)}
    

1 个答案:

答案 0 :(得分:0)

原因是url不对,在服务器上,发送参数应该是这样的:

user: admin
url: /location(right) vs /queue/location(wrong)

在客户端,subribe应该是这样的:

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