首页 > 解决方案 > 在 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/... 这不是我想要的。

我问的可能吗?

标签: springspring-bootwebsocketspring-websocketstomp

解决方案


推荐阅读