Spring Boot 中的多播 Websocket

Posted

技术标签:

【中文标题】Spring Boot 中的多播 Websocket【英文标题】:Multicast Websockets in Spring Boot 【发布时间】:2018-06-16 15:52:43 【问题描述】:

上下文

春季第一次使用 websockets。我的应用程序涉及一种类型的用户 (CHAP),将他们当前的位置提供给所有其他订阅 (USR)(并被授权订阅此信息)的用户

我正在阅读文档并找到 this 部分,我相信它包含我的解决方案,但我只是不能 100% 确定它是如何工作的,并且希望有更深入了解的人将其放入也就是说。我在堆栈溢出方面看到过类似的问题,但解决方案感觉太具体了(尽管这可能只是我自己缺乏理解)。

问题

每个主题一个 CHAP 可以将他们的位置发布到主题。 用户可以订阅他们有权订阅的任何主题。

所以本质上:

可变端点上的多个主题(类似于 /route_id/location )

用户可以订阅这些主题并在可用时接收更新

具有 CHAP 角色的用户可以发布到一个主题。 (即每个 CHAP 都有一个可以发布到的 route_id。

具有 USR 角色的用户可以收听他们所属的多个主题(即每个 USR 都有多个可以收听更新的路由)

这类似于拥有多个聊天室的问题,这是 websocket 的常见示例。但是,我能找到的所有示例要么具有静态聊天室名称、单个聊天室,要么只能将消息定位到一个用户(而不是组)

当前代码

@MessageMapping("/chaperone/location") // chaperone sends data to here
@SendTo("/route_id/location") // users can listen in on this
public BusModel updateLocation(@DestinationVariable long route_id, BusModel busModel) 
    return routeService.updateBusLocation(busModel);

我的想法是,伴侣发布到该网址,所有订阅其路线的用户都会得到更新。

谢谢!

【问题讨论】:

组 websockets 是什么意思?您的意思是一次向多个用户广播,即一个主题而不是队列? @123 对我来说可能措辞不好。 A similar question。比如拥有多个用户可以订阅的聊天室,并且只能从这些聊天室获取更新。 @123 完全正确!我可以找到的所有示例都针对 1. 单个用户(单播)或 2. 所有用户(广播),但我正在寻找多播风格的通信 你在使用 STOMP 吗? @123 我想使用 STOMP,因为它受到 spring 的高度支持。 【参考方案1】:

This 原来是一个解决方案,并且不需要像我之前想的那样进行太多配置。这是我的版本:

WebSocket配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer 

    public static final String TOPIC="/topic";

    /**
     * Consumers connect to endpoint/live to connect to the websocket
     *
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) 
        registry.addEndpoint("/live").setAllowedOrigins("*").withSockJS();
    

    /**
     * Once connected to the websocket, users can subscribe to endpoints prefixed with /topic
     * as these are managed by the message broker.
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) 
        registry.enableSimpleBroker(TOPIC);
        registry.setApplicationDestinationPrefixes("/");
    

LiveController

@SubscribeMapping(TOPIC + "/routes/route_id")
public MessageWrapper subscribeToRouteLocation(
        @DestinationVariable(value = "route_id") long route_id) 
    LOG.debug("New user subscribed to location of route %d", route_id);
    return new MessageWrapper(LOCATION_MESSAGE, routeService.getBusForRoute(route_id));


@MessageMapping("/routes/route_id")
@SendTo(TOPIC + "/routes/route_id")
public MessageWrapper updateRouteLocation(
        @DestinationVariable(value = "route_id") long route_id,
        @Payload BusStatusUpdateModel busLocation) 
    if (busLocation.getLat() == 0 && busLocation.getLon() == 0) 
        LOG.debug("Ending route %d", route_id);
        return new MessageWrapper(ROUTE_TERMINATED_MESSAGE, routeService.endBusForRoute(route_id));
     else 
        LOG.debug("Updating location of route %d", route_id);
        BusStatusUpdateModel statusUpdateModel = routeService.updateBusLocation(busLocation, route_id);
        return new MessageWrapper(LOCATION_MESSAGE, statusUpdateModel);
    

所以发送到 /routes/route_id 的消息将被传递给 /topic/routes/route_id 的订阅者

我还没有测试授权的东西,一旦我得到它就会填写!

【讨论】:

以上是关于Spring Boot 中的多播 Websocket的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 中的多对多映射问题

OPC UA 发现中的多播 DNS 名称冲突

spring boot jpa中的多对一映射中的外键未在子表中更新

带有 Spring Boot 的多模块 Web 应用程序 Maven 架构

Delphi的基于接口(IInterface)的多播监听器模式(观察者模式 ),利用RTTI实现Delphi的多播事件代理研究

使用Spring Boot Gradle插件的多模块项目