I stuck with following issue on websockets in spring 4, not sure why this code:
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000);
return new Greeting("Hello, " + message.getName() + "!");
}
works fine, and why this does not:
@MessageMapping("/hello")
public void hehe(HelloMessage message){
try {
greeting(message);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000);
return new Greeting("Hello, " + message.getName() + "!");
}
I'm looking for solution how to call greeting()
method if an event on server side occurred.
Separating them out will not work!!
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000);
return new Greeting("Hello, " + message.getName() + "!");
}
On a server side event if you want to send to a destination, you should be using:
simpMessagingTemplate.convertAndSend("/user/" + username + "/topic/greetings",
new Greeting("Hello, " + message.getName() + "!"));
// username should refer to the user in socket header if you want to send to a specific user
// omit the prefix /user/<username> if you are broadcasting
where SIMP is used (you can use messaging uitlities like rabbitMQ
too):
@Autowired
org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate;