Вопрос

Ok here i'm , i'm right now following the guides on spring site but i'm having problem on how to deliver a notification to only one user using WebSocket, i'm following this guide https://spring.io/guides/gs/messaging-stomp-websocket/ . What I want is: i have 2 users, both of them subscribe to process1... User1 need to let the server process his pass...now i want that the server will deliver the notification only to User1...

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

Now what should i write in the @SendTo field to deliver the answer only to user1 ? if ill write /Process/process1 both user1 and user2 will receive the message....

Это было полезно?

Решение

You ca use the sufix. It's send to every unique client.

When you subscribe in the js code:

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
  });

});

On the server side you can use @ReplyToUser or the message template

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

See more here: http://assets.spring.io/wp/WebSocketBlogPost.html (section: Sending Messages To a Single User)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top