Record Class WebSocketMessagingTemplate

java.lang.Object
java.lang.Record
cn.herodotus.engine.message.websocket.messaging.WebSocketMessagingTemplate

public record WebSocketMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate, org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry) extends Record

Description: WebSocket 消息发送模版

单独抽取一个 MessagingTemplate 用作 WebSocket 消息发送的基本操作类。 MultipleInstanceMessageSenderSingleInstanceMessageSenderMultipleInstanceMessageSyncConsumer 三个类均使用该类进行基础操作。 这样做的原因是多实例情况下还包含消息的同步,发送消息同步的实例既是同步消息的生产者,又是消费者。如果统一使用 WebSocketMessageSender 注入的 Bean。就不好区分各种情况,就会出现发送同步消息,存在循环发送消息的风险。

Author:
: gengwei.zheng
Date:
: 2023/10/26 23:26
  • Constructor Summary

    Constructors
    Constructor
    Description
    WebSocketMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate, org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry)
    Creates an instance of a WebSocketMessagingTemplate record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    broadcast(String destination, Object payload)
    发送 WebSocket 广播消息。发送全员信息
    final boolean
    Indicates whether some other object is "equal to" this one.
    org.springframework.messaging.simp.user.SimpUser
    getUser(String userId)
    根据用户 ID 获取到对应的 WebSocket 用户
    final int
    Returns a hash code value for this object.
    boolean
    判断 WebSocket用户是否存在。
    void
    pointToPoint(String user, String destination, Object payload)
    发送 WebSocket 点对点消息。发送信息给指定用户
    org.springframework.messaging.simp.SimpMessagingTemplate
    Returns the value of the simpMessagingTemplate record component.
    org.springframework.messaging.simp.user.SimpUserRegistry
    Returns the value of the simpUserRegistry record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • WebSocketMessagingTemplate

      public WebSocketMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate, org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry)
      Creates an instance of a WebSocketMessagingTemplate record class.
      Parameters:
      simpMessagingTemplate - the value for the simpMessagingTemplate record component
      simpUserRegistry - the value for the simpUserRegistry record component
  • Method Details

    • pointToPoint

      public void pointToPoint(String user, String destination, Object payload)
      发送 WebSocket 点对点消息。发送信息给指定用户
      Parameters:
      user - 用户唯一标识
      destination - 消息同奥
      payload - 消息内容
    • broadcast

      public void broadcast(String destination, Object payload)
      发送 WebSocket 广播消息。发送全员信息
      Parameters:
      destination - 消息同奥
      payload - 消息内容
    • getUser

      public org.springframework.messaging.simp.user.SimpUser getUser(String userId)
      根据用户 ID 获取到对应的 WebSocket 用户
      Parameters:
      userId - 系统用户ID
      Returns:
      WebSocket 用户 SimpUser
    • isUserExist

      public boolean isUserExist(String userId)
      判断 WebSocket用户是否存在。

      注意:只能查询到当前所在 WebSocket实例中的实时 WebSocket 用户信息。如果实时用户在不同的实例中,则查询不到。

      Parameters:
      userId - 用户ID
      Returns:
      true 用户存在,false 用户不存在
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • simpMessagingTemplate

      public org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate()
      Returns the value of the simpMessagingTemplate record component.
      Returns:
      the value of the simpMessagingTemplate record component
    • simpUserRegistry

      public org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry()
      Returns the value of the simpUserRegistry record component.
      Returns:
      the value of the simpUserRegistry record component