gerrytan对在SpringWebsocket上向特定用户发送消的回答提到了Web套接字配置更改,以注册/user
前缀。就您而言,我想这意味着要更换
registry.enableSimpleBroker("/topic", "/queue");
与
registry.enableSimpleBroker("/topic", "/queue", "/user");
他还说,在控制器中,您不需要/user
前缀,因为它是自动添加的。因此,您可以尝试以下操作:
template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications);
和这个:
template.convertAndSendToUser(principal.getName(), "/user/queue/notification", notifications);
在客户端,您需要提供用于连接服务器的用户名。您可以直接将其插入:
stompClient.subscribe('/user/naila/queue/notification', ...)
或从标题获取它。但是马库斯在“如何向特定用户发送网络套接字消息”中)说?即使在这里您也不需要用户名,因此它可能像这样工作:
stompClient.subscribe('/user/queue/notification', ...)