spring - 在 STOMP 上返回多个对象订阅 Spring Websockets
问题描述
我有一个话题/topic/routes/{routeid}
用户订阅该主题,并使用以下控制器方法获取该路线上公共汽车的当前位置:
@SubscribeMapping(TOPIC + "/routes/{routeId}")
public MessageWrapper<BusStatusResponseModel> subscribeToRouteLocation(
@DestinationVariable(value = "routeId") short routeId) {
return new MessageWrapper<>(LOCATION_MESSAGE, routeService.getBusForRoute(routeId));
}
但是,我想尝试将我的应用程序初始化移动到 websockets 而不是 REST,所以在订阅时,我想返回多个单独的消息。例如,路线上的停靠点、这条路线的先前记录等。
我试过让订阅控制器方法返回 void,而是像这样使用 SimpMessagingTemplate:
@SubscribeMapping(TOPIC + "/routes/{routeId}")
public void subscribeToRouteLocation(@DestinationVariable(value = "routeId") short routeId, Principal principal) {
messagingTemplate.convertAndSendToUser(
user.getName(),
ROUTES,
new MessageWrapper<>(LOCATION_MESSAGE,
routeService.getBusForRoute(routeId))
);
final LocalDate localDate = dateService.tenantTime().toLocalDate();
messagingTemplate.convertAndSendToUser(
user.getName(),
ROUTES,
new MessageWrapper<>(
MessageWrapper.ROUTE_ACTIVITY_SUBSCRIPTION_MESSAGE,
activityService.getAllRouteReportsForRouteInDateRange(routeId, localDate.minusDays(7), localDate))
);
}
但是,这发送到 /user/... 这不是我想要的。
我问的可能吗?
解决方案
推荐阅读
- ecmascript-6 - 从 ES6 箭头函数返回对象文字
- java - Java 11 中的 VertX 服务代理引发 TimeTrackingProcessor 警告
- c# - 在后端使用 TFL api - C#,在前端使用 React.js
- javascript - 如何动态(通过路径)更新 React 状态属性
- javascript - JavaScript 中的石头剪刀布游戏。无限循环问题
- flutter - Dart jsonEncode 给我错误 Unhandled Exception: Converting object to an encodable object failed
- java - 如何在 java 中获得对 http 的响应?
- javascript - 客户端 JS 函数中 Facebook / Instagram 的访问令牌
- java - Libgdx 使 scrollPane 不需要单击交互来启动滚动
- r - 带有百分比的 R 饼图