在您的Websocket控制器中,您应该执行以下操作:
@Controllerpublic class GreetingController { @Autowired private SimpMessagingTemplate messagingTemplate; @MessageMapping("/hello") public void greeting(Principal principal, HelloMessage message) throws Exception { Greeting greeting = new Greeting(); greeting.setContent("Hello!"); messagingTemplate.convertAndSendToUser(message.getToUser(), "/queue/reply", greeting); }}在客户端,您的用户应订阅主题/ user / queue / reply。
您还必须添加一些目标前缀:
@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic", "/queue" ,"/user"); config.setApplicationDestinationPrefixes("/app"); config.setUserDestinationPrefix("/user"); }}当服务器在/ app / hello队列上收到消息时,它应该向dto中的用户发送消息。用户必须等于用户的主体。
我认为您代码中的唯一问题是您的“ /用户”不在目标前缀中。您的问候语消息被阻止,因为您将其发送到以/ user开头的队列中,并且未注册此前缀。
您可以在git repo上查看源代码:https : //github.com/simvetanylen/test-spring-
websocket
希望它能起作用!



