I have the following contoller
@Controller
public class GreetingController
{
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Person greeting(String message) throws Exception {
Person person=new Person();
person.setAge(10);
return person;
}
@Autowired
private SimpMessagingTemplate template;
@RequestMapping(path="/meeting",method=RequestMethod.POST)
public @ResponseBody void greet() {
this.template.convertAndSend("/topic/greetings", "message");
}
}
and my configuration is
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig1 extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
So according to spring doc template.convertAndSend("/topic/greetings", "message") should call the broker and the mapped web socket will be called.
Code for front-end using SockJS
var socket = new SockJS('/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function(greeting){
console.log(JSON.parse(greeting.body));
});
// to send via the web service-WORKING ( but websocket not called in springs)
$.post("http://localhost:8080/meeting");
// to send via websocket - WORKING
stompClient.send("/app/hello", {}, JSON.stringify({ 'message':'message'}));
There are no errors in the console. I can connect it through SockJs and send message to "/topic/greetings" but i want to call a webService which in return calls the web Socket. So after searching alot i m stuck cause there are no errors and cant find a different way to do it in spring.