Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call @SendTo from Normal Request Call i.e @RequestMapping

I have implemented Web Socket using Spring MVC and it is working fine for me i.e work from one browser to another browser which is open for those socket using this code.

@MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public HelloMessage greeting(HelloMessage message) throws Exception {
        Thread.sleep(3000); // simulated delay
        return message;
    }

Can any one help me for who to call @SendTo("/topic/greetings") from normal api controller.I have try using this but it is not working for me

@RequestMapping(value = "/sendMessage")
    @SendTo("/topic/greetings")
    public HelloMessage sendMessage() throws Exception {
        return new HelloMessage((int) Math.random(), "This is Send From Server");
    }

any idea for this?

Thanks

like image 786
Charnjeet Singh Avatar asked Jul 24 '15 10:07

Charnjeet Singh


People also ask

What is the @RequestMapping annotation used for?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What is @RequestMapping annotation in Spring boot?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.

Which of the following is the method where RequestMapping is used with RequestParam?

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method.

Which of these can be the return type of a method annotated with @RequestMapping in Spring?

A Callable can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC. A DeferredResult can be returned when the application wants to produce the return value from a thread of its own choosing.


1 Answers

I have found solution for that

@Autowired
private SimpMessagingTemplate template;


@RequestMapping(value = "/sendMessage")
public void sendMessage() throws Exception {
    this.template.convertAndSend("/topic/greetings", new HelloMessage(
            (int) Math.random(), "This is Send From Server"));
}

by using this we can send message to open WebSocket.

Thanks

like image 54
Charnjeet Singh Avatar answered Oct 01 '22 00:10

Charnjeet Singh