使用 Spring Websocket 在目标主题中设置 principal.getName()
Set principal.getName() in destination topic with Spring Websocket
我正在使用 spring WebSocket,但我不知道如何将消息发送到包含用户名的特定分区。
即:
@MessageMapping("/getNotifications")
@SendTo("/topic/notifications")
public @ResponseBody List<NotificationResponse> getNotificationsWS(int size,
SimpMessageHeaderAccessor headerAccessor, Principal principal) throws Exception {
ActionResult<List<NotificationResponse>> notificationsAr;
notificationsAr = this.listNotifications(size, principal);
return notificationsAr.getResult();
}
我正在尝试将消息发送到“/topic/notifications/myUserName”而不是“/topic/notifications”。通过这种方式,每个登录用户都从他自己的分区中消费。
我该怎么做?
尝试注入SimpMessagingTemplate
然后就可以simpMessagingTemplate.convertAndSendToUser(username, "/queue/reply", message);
看看Thanh post.
我正在使用 spring WebSocket,但我不知道如何将消息发送到包含用户名的特定分区。
即:
@MessageMapping("/getNotifications")
@SendTo("/topic/notifications")
public @ResponseBody List<NotificationResponse> getNotificationsWS(int size,
SimpMessageHeaderAccessor headerAccessor, Principal principal) throws Exception {
ActionResult<List<NotificationResponse>> notificationsAr;
notificationsAr = this.listNotifications(size, principal);
return notificationsAr.getResult();
}
我正在尝试将消息发送到“/topic/notifications/myUserName”而不是“/topic/notifications”。通过这种方式,每个登录用户都从他自己的分区中消费。
我该怎么做?
尝试注入SimpMessagingTemplate
然后就可以simpMessagingTemplate.convertAndSendToUser(username, "/queue/reply", message);
看看Thanh post.