2017-10-12 8 views
-1

私は登録ユーザーを持っているWebアプリケーションを持っています。ユニークなユーザーIDを持つユーザーにメッセージをプッシュしたいと思います。クライアントにメッセージを送信できるようにするために、私は@sendto注釈に固有のユーザーIDを渡す方法を知っておく必要がありSpringフレームワークのWebソケット固有の@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); 
     }); 
です

どのように私はあなたがそのように、メソッドの引数に@DestinationVariableアノテーションを使用することができます@SendTo("/topic/uniqueuserid")

+1

おそらくhttps://stackoverflow.com/questions/27047310/path-variables-in-spring-websockets-sendto-mapping –

答えて

1

に固有のユーザーIDを渡すことができます:

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