2017-10-12 97 views
-1

我有一个网络应用程序已注册的用户,我想推动消息具有唯一用户ID的用户。为了能够将消息发送到客户端,我需要知道如何通过独特的用户ID在@sendto注释Spring框架的网络套接字唯一@SendTo注释

这是注释

@SendTo("/topic/uniqueuserid") 
    public Greeting greeting(HelloMessage message) throws Exception { 
     int uniqueuserid; 
     Thread.sleep(1000); // simulated delay 
     return new Greeting("Hello, " + message.getName() + uniqueuserid "!"); 
} 

和这个跺脚JS

stompClient.subscribe('/topic/uniqueuserid', function (greeting) { 
      showGreeting(JSON.parse(greeting.body).content); 
     }); 

如何传递唯一的用户标识@SendTo("/topic/uniqueuserid")

+1

也许https://stackoverflow.com/questions/27047310/path-variables-in-spring-websockets-sendto-mapping – 2017-10-12 09:56:26

回答

1

您可以在方法参数中使用@DestinationVariable注释,例如:

@MessageMapping("/mychat/{uniqueUserId}") 
@SendTo("/topic/{uniqueUserId}") 
public Message message(@DestinationVariable("uniqueUserId") Long uniqueUserId, HelloMessage message) { 
    logger.info("Unique user id: {}", uniqueUserId); 
}