我设法找到了一种仅根据条件发送更新的方法
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping("/comment/{id}" )
public OperationResult<Comment> comment(Comment comment) throws Exception {
OperationResult<Comment> operationResult = new OperationResult<>();
conversationService.comment(operationResult, comment);
if (operationResult.isSuccess()) {
simpMessagingTemplate.convertAndSend("/topic/conversation/"+operationResult.getReturnedObject().getConversation().getId(), operationResult);
}
return operationResult;
}
但是,这不允许我将operationResult返回给客户端。
更新:
为了让客户端获得operationResult我@MessageMapping
用@RequestMapping
. 所以客户端必须进行正常的 ajax 调用才能提交评论。
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@RequestMapping("/comment" )
public OperationResult<Comment> comment(@RequestBody Comment comment) throws Exception {
OperationResult<Comment> operationResult = new OperationResult<>();
conversationService.comment(operationResult, comment);
if (operationResult.isSuccess()) {
simpMessagingTemplate.convertAndSend("/topic/conversation/"+operationResult.getReturnedObject().getConversation().getId(), operationResult);
}
return operationResult;
}