【发布时间】:2024-01-22 09:23:01
【问题描述】:
我在 Springboot 上使用 STOMP websocket 并希望将广播限制到特定页面。这是我的过程:
-
User将消息填充到 HTML 输入中。 - 浏览器会通过STOMP客户端发送消息。
- 服务器接收消息并对其进行验证。如果消息有效,它将广播到
User已发送消息的用户处理的所有选项卡。如果它无效,它将仅将错误消息发送回发送该消息的特定浏览器标签,而不是其他标签,即使这些标签具有相同的User登录。
虽然我不能限制将错误消息发送到特定选项卡,但我已经使它的某些部分工作,它总是将错误消息广播到共享相同User 的所有选项卡。这是我的初始代码:
@MessageMapping("/api/secure/message")
@SendToUser("/api/secure/broadcast")
public HttpEntity createMessage(Message message, Authentication authentication) throws Exception {
Set<String> errors = TreeSet<String>();
// Process Message message and add every exceptions encountered to Set errors.
boolean valid = (errors.size() > 0);
if(valid) {
// Broadcast to all.
return new ResponseEntity(message, HttpStatus.OK);
}
else {
// Send the message to that particular tab only.
return new ResponseEntity(errors, HttpStatus.UNPROCESSABLE_ENTITY);
}
}
这可以通过websocket 实现吗?还是应该回XHR?
【问题讨论】:
标签: spring-boot websocket stomp