はじめに
突然、暑くなりましたね。
私のような素人のブログでは、ただの日記のようになっていますが、久しぶりに更新しました。
動作環境
Spring Boot 2.7.3
Java SE11
実装しようとしたこと
チャットルームの実装です。
前回試したチュートリアルを用いて作成しました。
@MessageMapping("/hello/{id}")
@SendTo("/topic/greetings/{id}" )
public Greeting greeting(HelloMessage message,@PathVariable String id) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}
@GetMapping("/topic/greetings/{id}")
public String messageIndex(@PathVariable String id, Model model) {
model.addAttribute("id", id);
return "index";
}
"/topic/greetings/{id}"へアクセスした際にidがstompClient.sendでの宛先にセットできるようにし、stompClient.subscribeで/topic/greetings/{id}に送られたメッセージを受け取るという形式にしました。