首页 文章

spring websocket通知

提问于
浏览
1

好的,我现在按照 spring 网站上的指南进行操作,但是我按照本指南https://spring.io/guides/gs/messaging-stomp-websocket/进行了操作 . 我想要的是:我有2个用户,他们都订阅了process1 ... User1需要让服务器处理他的通行证...现在我希望服务器只向User1发送通知...

@Controller
public class ProcessController {
   @MessageMapping("/ProcessOwner/approve/{pass}")
   @SendTo("")
   public String notifica(@DestinationVariable String pass)throws Exception{
      return "ok"+pass;
   }
}

现在我应该在@SendTo字段中写什么来仅向user1提供答案?如果生病写入/进程/进程1,user1和user2都将收到消息....

1 回答

  • 0

    你可以使用sufix . 它发送给每个独特的客户 .

    当您在js代码中订阅时:

    client.connect('guest', 'guest', function(frame) {
    
      var suffix = frame.headers['queue-suffix'];
    
      client.subscribe("/queue/error" + suffix, function(msg) {
        // handle error
      });
    
      client.subscribe("/queue/position-updates" + suffix, function(msg) {
        // handle position update
      });
    
    });
    

    在服务器端,您可以使用 @ReplyToUser 或消息模板

    String user = "fabrice";
    String queue = "/queue/position-updates";
    this.messagingTemplate.convertAndSendToUser(user, queue, position);
    

    在此处查看更多:http://assets.spring.io/wp/WebSocketBlogPost.html(部分:向单个用户发送消息)

相关问题