Disallow broadcast in convertAndSendToUser method in SimpMessagingTemplate

女生的网名这么多〃 提交于 2019-12-12 08:01:29

问题


I am working with Spring websocket implementation. For sending a message to clients, there are two ways:

1) Using @SendToUser annotation
2) Using convertAndSendToUser method of SimpMessagingTemplate

@SendToUser takes a boolean parameter called broadcast which if set to false publishes the message to the current session. Is there a way I can have this behaviour in SimpMessagingTemplate.


回答1:


If we take a look to the SendToMethodReturnValueHandler source code, we'll see:

if (broadcast) {
    this.messagingTemplate.convertAndSendToUser(user, destination, returnValue);
}
else {
    this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId));
}

So, what you need for your use-case just use that overloaded convertAndSendToUser and provide a Map with `sessionId:

messagingTemplate.convertAndSendToUser(user, destination, payload, 
           Collections.singletonMap(SimpMessageHeaderAccessor.SESSION_ID_HEADER, sessionId))



回答2:


Spring doesn't have a clear document, I tried many different way, only below code works for me.

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setHeader(SimpMessageHeaderAccessor.SESSION_ID_HEADER, sessionId);
messagingTemplate.convertAndSendToUser(sessionId, destination, payload, accessor.getMessageHeaders());



回答3:


The answer above did not work for me. It turns out that with Spring 4.1.4 something slightly different is required.

The way that seems the cleanest to me looks like the following:

SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
headerAccessor.setSessionId(cmd.getSessionId());
headerAccessor.setLeaveMutable(true);
MessageHeaders messageHeaders = headerAccessor.getMessageHeaders();

messagingTemplate.convertAndSendToUser(cmd.getPrincipal().getName(),
       "/queue/responses", ret, messageHeaders);

The other way which worked was to explicitly add a "nativeHeaders" value to the Map sent to SimpMessagingTemplate.convertAndSendToUser(). However, this way seems to depend too much on implementation details:

Map<String, Object> headers = new HashMap<>();
headers.put("nativeHeaders", new HashMap<String, Object>());
headers.put(SimpMessageHeaderAccessor.SESSION_ID_HEADER, cmd.getSessionId());

messagingTemplate.convertAndSendToUser(cmd.getPrincipal().getName(),
        "/queue/responses", ret, headers);

The "offending code" which made setting the "simpSessionId" header and nothing else in a Map not work was in SimpMessagingTemplate.processHeaders() and MessageHeaderAccessor.getAccessor(MessageHeaders, Class requiredType).




回答4:


The simplest way send to User by SimpMessagingTemplate

@Autowired
private SimpMessagingTemplate messagingTemplate;

@MessageMapping("/getHello")
public void sendReply( MessageHeaders messageHeaders, @Payload String message, @Header(name = "simpSessionId") String sessionId){
        messagingTemplate.convertAndSendToUser(sessionId, "/queue/hello", "Hello "+ message, messageHeaders);
}


来源:https://stackoverflow.com/questions/28068622/disallow-broadcast-in-convertandsendtouser-method-in-simpmessagingtemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!