Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringWebSocket通知_Spring_Websocket - Fatal编程技术网

SpringWebSocket通知

SpringWebSocket通知,spring,websocket,Spring,Websocket,好的,我在这里,我现在正在遵循spring站点上的指南,但是我在如何使用WebSocket只向一个用户发送通知方面遇到了问题,我遵循这个指南。 我想要的是:我有2个用户,他们都订阅process1。。。User1需要让服务器处理他的通行证…现在我希望服务器只向User1发送通知 @Controller public class ProcessController { @MessageMapping("/ProcessOwner/approve/{pass}") @SendTo("")

好的,我在这里,我现在正在遵循spring站点上的指南,但是我在如何使用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都会收到消息……

您可以使用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);

请参阅此处的更多内容:(部分:向单个用户发送消息)

我找到了它,但在理解它时遇到了一些问题。。。我如何知道用户是“fabrice”?此.messagingTemplate.convertAndSendToUser(用户、队列、位置)//这一行发送给订阅了/queue/position updates的用户fabrice,但它如何知道这是您提到的前一个客户端?请看这里(第20.4.9节身份验证)。在较新的spring版本中,此行为似乎有所改变,spring在服务器端管理队列和后缀,并且不向客户端公开。也看到