该项目包含WebSocketConfiguration,端点为“ ws”,简单代理前缀为“ / topic”。
@Override
public void registerStompEndpoints(StompEndpointRegistry registry)
{
registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry)
{
registry.setApplicationDestinationPrefixes("").
enableSimpleBroker("/topic");
}
当我从角度组件的后端服务中调用post方法(“ / action”)时,消息将转换并发送到目标“ / topic / test”。
@PostMapping("/action/{id}")
public ResponseEntity<Information> action(HttpSession httpSession, @PathVariable(name = "id") String gameId,
@Valid @RequestParam("action") Action action) {
try
{
Information information = gameService.handle(httpSession.getId(),
UUID.fromString(gameId), action);
messagingTemplate.convertAndSend("/topic/test", "go");
return new ResponseEntity<>(information, HttpStatus.OK);
} catch (HttpClientErrorException e) {
return new ResponseEntity<>(e.getStatusCode());
}
}
在角度组件中,websocket的结果不会更新,就像消息没有发送到目的地一样。 组件的构造函数中的连接:
constructor(private route: ActivatedRoute, private gameService: GameService) {
this.route.params.subscribe(params => {
this.id = params['id'];
});
const socket = new SockJS(`http://192.168.0.16:8080/ws`);
let stomp = Stomp.over(socket);
stomp.connect('', function(frame) {
stomp.subscribe(`/topic/test`, res => {
console.log(res);
});
});
}
更新: 我有解决办法。我只是将端点从“ ws”更改为“ socket”,并且可以正常工作。我不知道为什么它在起作用吗?对我来说这是一个非常有趣的情况,我想知道为什么。