【问题标题】:spring websocket notification春季websocket通知
【发布时间】:2014-05-11 15:47:53
【问题描述】:

好的,我在这里,我现在正在关注 spring 网站上的指南,但我在如何使用 WebSocket 仅向一个用户发送通知时遇到问题,我正在关注本指南 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 ?如果写错 /Process/process1 user1 和 user2 都会收到消息....

【问题讨论】:

    标签: spring websocket


    【解决方案1】:

    你可以使用后缀。它发送给每个独特的客户。

    在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(部分:向单个用户发送消息)

    【讨论】:

    • 我找到了它,但我在理解它时遇到了一些问题......我怎么知道用户是“fabrice”? this.messagingTemplate.convertAndSendToUser(user, queue, position); //这条线发送给订阅了/queue/position-updates的用户fabrine,但它怎么知道它是你提到的前一个客户端?
    • 在最近的 spring 版本中,这种行为似乎有所改变,spring 在服务器端管理队列和后缀,并且不暴露给客户端。另见stackoverflow.com/questions/26991832
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多