添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am new to Spring boot websocket and messaging semantics. Currently i am able to send private messages using the below code.

String queueName = "/user/" + username  + "/queue/wishes";
simpMessagingTemplate.convertAndSend(queueName, message);

When trying to use convertAndSendToUser I am not getting any error but the message is not getting sent. I knew that with sendToUser there should be a slight change in how the destination name should be formed but I am not getting it right.

String queueName = "/user/queue/wishes";
simpMessagingTemplate.convertAndSendToUser(username, queueName, message);

Below is my subscription code.

stompClient.subscribe('/user/queue/wishes', function(message) {
    alert(message);

I had a similar problem, if your username is actually a sessionId, then try to use one of the overloaded methods that accept headers (so said in SimpMessageSendingOperations javadoc):

SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headerAccessor.setSessionId(username);
headerAccessor.setLeaveMutable(true);
messagingTemplate.convertAndSendToUser(username, "/queue/wishes", message, headerAccessor.getMessageHeaders());

my example

It is not getting sent because your "destination" i.e "queuename" is incorrect. As you can see here SimpMessagingTemplate.java#L230 this is the method that gets invoked as a part of the chain on invocations of template.convertAndSendToUser().

This method already prefixes /user to the final destination. So instead you should do something like this: simpMessagingTemplate.convertAndSendToUser(username,"/queue/wishes", message); Now with the correct destination it should get sent to user.

No. It's still not working. I ended up adding "/user" to the destination after initially, it didn't work with "/queue/wishes" destination. – Harsha Jun 21, 2020 at 5:17

You must registry the prefix of SimpleBroker:

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableSimpleBroker("/topic", "/queue", "/user");
    registry.setUserDestinationPrefix("/user");

Although the UserDestinationPrefix has been set for default value "/user/", you must to add the SimpleBroker prefix.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.