I found an example of a web socket from the Internet and I had a question that will help me in the implementation of my project
@Controller
@SendTo("/topic/greetings")
public class GreetingController {
@MessageMapping("/hello")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, name " + HtmlUtils.htmlEscape(message.getName()) + "!");
}
}
in this controller there is a name exchange and everything works correctly
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
}
}
in my project I want to use rest api which will call the method in the controller
@Controller
@RequestMapping("/name")
public class SampleController {
GreetingController greetingController;
public SampleController() {
greetingController = new GreetingController();
}
@PostMapping
public void name(@RequestBody HelloMessage helloMessage) throws Exception {
greetingController.greeting(helloMessage);
}
}
When I call the method on api, I try to send a value on the socket so that everyone who subscribes receives this value. The controller that I wrote is an example. Is this possible?thanks in advance