Spring Boot - Websockets - 如何查看订阅者 [英] Spring Boot - Websockets - How to see subscribers

查看:33
本文介绍了Spring Boot - Websockets - 如何查看订阅者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a websocket application where I'm trying to have one websocket that feeds information in, and then outputs to subscribers to the endpoint. I've figure that out, but I'm wondering if there is a way to see what subscribers are subscribed and to what path?

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

Here is a code sample of what I'm working on.

@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> 从此映射中检索.

The idea is that the information poster sends companyMessage object to the @MessageMapping endpoint, the companyId and departmentId are retrieved from this mapping.

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

This message is then processed based on what the departmentId is and is posted to every department that has an active subscriber to the @SendTo path.

例如

有 3 个 websocket 订阅者,/smallCompany/booking/smallCompany/sales/smallCompany/inventory.
@MessageMapping/smallCompany/sales 获取消息.该方法根据 departmentId 处理消息,并使用相同的 /{companyId} 发布给每个订阅者,即使 /{departmentId}不同.

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.

如果可能的话,任何想法,如果没有,任何将我推向正确方向的想法都会很棒.

Any ideas if this is possible, and if not, any ideas to push me in the right direction would be great.

推荐答案

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

I know it's too late to answer! but I saw this question now! So, to guide others that will see this question, I should say:

您有几种解决方案:

1- SimpUserRegistry:

1- SimpUserRegistry:

@Autowired private SimpUserRegistry simpUserRegistry;

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

检查此链接:是否有 Spring WebSocketSession 存储库?

2- 全局列表:

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

You've certainly configured the web-socket in spring boot, so, probably you have a derived class from WebSocketMessageBrokerConfigurer, with override configureClientInboundChannel to call setInterceptors...

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

You should implement custom interceptor derived from ChannelInterceptorAdapter and override preSend to access MessageHeaderAccessor.getAccessor(...).command
These commands defined in StompCommand (CONNECT, DISCONNECT, SUBSCRIBE, UNSUBSCRIBE,...)

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

By checking accessor.command with StompCommand.SUBSCRIBE/UNSUBSCRIBE you can create and sync a global static list of subscribers, and use it everywhere you need.

3- 其他解决方案:

检查此链接:
如何在我的带有 Spring 4 的 webSocket 服务器

这篇关于Spring Boot - Websockets - 如何查看订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆