gpt4 book ai didi

java - Spring Boot 中的多播 Websocket

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:29 25 4
gpt4 key购买 nike

上下文

第一次使用 websockets 是在 Spring 。我的应用程序涉及一种类型的用户 (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);
}

我的想法是伴侣发帖到那个 url,所有订阅他们路线的用户都会得到更新。

谢谢!

最佳答案

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("/");
}
}

直播 Controller

@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} 的订阅者

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

关于java - Spring Boot 中的多播 Websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50889417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com