Spring websocket:如何发送给除消息发送者之外的所有订阅者 [英] Spring websocket: how to send to all subscribers except the message sender

查看:51
本文介绍了Spring websocket:如何发送给除消息发送者之外的所有订阅者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sockJs 和 Stomp 遵循 Spring websocket 的快速入门指南:https://spring.io/guides/gs/messaging-stomp-websocket/

I am following the quick-start guide on Spring websocket with sockJs and Stomp here: https://spring.io/guides/gs/messaging-stomp-websocket/

此时,我的代码看起来像指南中的代码并且按预期工作.我有一个控制器类,它有一个方法接受传入的消息并将它们发送回所有订阅该主题的人.

At this point, my code looks like to one from guide and works as intended. I have a controller class with a method accepting incoming messages and sending them back to all who subscribed on the topic.

我想要做的是更改代码,所以我的 @MessageMapping 注释方法向所有订阅者发送响应 不包括首先向控制器发送消息的订阅者(因为发件人也订阅了相同的主题,但我不希望发件人继续接收它自己发送的消息,我猜这是一种循环).

What I want to do, is to change the code, so my @MessageMapping annotated method sends response to all subscribers excluding the one who send the message to the controller in the first place (because the sender is also subscribed to the same topic, but i dont want the sender to keep receiving messages it send itself, it is kind of a loop I guess).

我看过许多描述如何向单个订阅者发送消息的文档,但还没有看到描述如何向所有订阅者发送消息 - 初始消息发送者.

I have seen many docs describing how to send a message to a single subscriber, but have not yet seen on describing how to send to all but one - the initial message sender.

在 Spring websocket 中是否有任何内置的方法可以轻松地做到这一点?

Is there any built-in way to do this easily in Spring websocket?

推荐答案

好的,所以我已经设法找到了一些目前对我有用的解决方案:

Ok so i've managed to find some solution which works for me at this point of time:

我能够按主要用户名过滤订阅者.

i was able to filter subscribers by principal user name.

我从 org.springframework.messaging.simp.user.SimpUserRegistry 中获取了所有 simp 用户,以及来自 org.springframework.messaging.simp.stomp.StompHeaderAccessor 的当前发件人.

I got all simp users form org.springframework.messaging.simp.user.SimpUserRegistry, and a current sender from org.springframework.messaging.simp.stomp.StompHeaderAccessor.

我的代码如下所示:

  @MessageMapping("/game/doStuff")
  public void gameGrid(DoStuffMessage doStuffMessage,
      StompHeaderAccessor headers) {
    sendTo("/game/doStuff", doStuffMessage, headers);
  }

  private void sendTo(String destination, Object payload, StompHeaderAccessor headers) {
    Optional<String> user = Optional.ofNullable(headers.getUser())
        .map(Principal::getName);

    if (user.isPresent()) {
      List<String> subscribers = simpUserRegistry.getUsers().stream()
          .map(SimpUser::getName)
          .filter(name -> !user.get().equals(name))
          .collect(Collectors.toList());

      subscribers
          .forEach(sub -> simpMessagingTemplate.convertAndSendToUser(sub, destination, payload));
    }
  }

客户正在订阅/user/game/doStuff

它现在有效.我担心的是这段代码是否可以横向扩展 - 如果有人对此有任何见解,我将不胜感激.

It works for now. What I am worried about is if this code can scale horizontally - if someone has any insight on this I'd greatly appreciate that.

这篇关于Spring websocket:如何发送给除消息发送者之外的所有订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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