gpt4 book ai didi

spring-boot - Spring Boot - Websockets - 如何查看订阅者

转载 作者:行者123 更新时间:2023-12-05 08:56:01 24 4
gpt4 key购买 nike

我正在开发一个 websocket 应用程序,我试图让一个 websocket 提供信息,然后输出到端点的订阅者。我已经弄清楚了,但我想知道是否有办法查看订阅了哪些订阅者以及订阅了哪些路径?

这是我正在处理的代码示例。

@Autowired
private SimpMessagingTemplate template;

@MessageMapping("/{companyId}/{departmentId}")
@SendTo("/{companyId}/{departmentId}")
public void companyInformation(@DestinationVariable String companyId, @DestinationVariable String departmentId, CompanyMessage companyMessage){

String destination = "/" + companyId + "/" + departmentId;
template.convertAndSend(
destination, processCompanyMessage(companyMessage, departmentId));
}

想法是信息发布者将companyMessage对象发送到@MessageMapping端点,companyIddepartmentId 从此映射中检索。

然后根据 departmentId 处理此消息,并将其发布到每个在 @SendTo 路径上有活跃订阅者的部门。

例如

There are 3 websocket subscribers, /smallCompany/booking, /smallCompany/sales, /smallCompany/inventory.
@MessageMapping gets a message from /smallCompany/sales. The method processes the message based on the departmentId and posts to EVERY subscriber with the same /{companyId}, even if the /{departmentId} differs.

任何想法,如果这是可能的,如果没有,任何能插入我朝着正确方向前进的想法都会很棒。

最佳答案

我知道现在回答已经来不及了!但我现在看到了这个问题!所以,为了引导其他人看到这个问题,我应该说:

您有几种解决方案:

1- SimpUserRegistry:

@Autowired private SimpUserRegistry simpUserRegistry;

public Set<SimpUser> getUsers() {
return simpUserRegistry.getUsers();
}

检查此链接: Is there a Spring WebSocketSession repository?

2- 全局列表:

您肯定已经在 spring boot 中配置了 web-socket,因此,您可能有一个来自 WebSocketMessageBrokerConfigurer 的派生类,并覆盖了 configureClientInboundChannel 以调用 setInterceptors...

您应该实现从 ChannelInterceptorAdapter 派生的自定义拦截器并覆盖 preSend 以访问 MessageHeaderAccessor.getAccessor(...).command
这些命令定义在 StompCommand (CONNECT, DISCONNECT, SUBSCRIBE, UNSUBSCRIBE,...)

通过使用 StompCommand.SUBSCRIBE/UNSUBSCRIBE 检查 accessor.command,您可以创建和同步全局静态订阅者列表,并在您需要的任何地方使用它。

3- 其他解决方案:

检查此链接:
how to capture subscribe event in my webSocket server with Spring 4

关于spring-boot - Spring Boot - Websockets - 如何查看订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42923461/

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