我有以下控制器
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
我的配置是
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,根据spring doc template.convertAndSend("/ topic/greetings","message")应该调用代理并调用映射的Web套接字.
使用SockJS的前端代码
var socket = new SockJS('/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: …Run Code Online (Sandbox Code Playgroud)