简体   繁体   中英

WebSocket convertAndSendToUser can not received message

I am using websocket to send message to client,when using this method broadcast message to client.

private void pushInfoImpl(String url, String content) {
    if (simpMessagingTemplate != null) {
        simpMessagingTemplate.convertAndSend(url, content);
    }
}

The client could received messsage successfully. When using this method to send specified user:

//send url:/queue/location
//user:admin
 private void pushInfoImpl(String user, String url, String content) {
    if (simpMessagingTemplate != null) {
        simpMessagingTemplate.convertAndSendToUser(user, url, content);
    }
}

The client can't receive the sent message. What I have tried:

  1. add queue in config.enableSimpleBroker("/topic","/queue");
  2. subscribe the url like stompClient.subscribe('/admin/queue/location', function (greeting) { console.log(greeting.body); }); .admin is the user.

What should I do to fix this problem? To make the client received server messsage successfully. This is log:

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

The reason is the url is not right,on the server,sending parameter should like this:

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

on the client,the subsribe should like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM