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 消息发送的基本操作类。
MultipleInstanceMessageSender、SingleInstanceMessageSender 和 MultipleInstanceMessageSyncConsumer 三个类均使用该类进行基础操作。
这样做的原因是多实例情况下还包含消息的同步,发送消息同步的实例既是同步消息的生产者,又是消费者。如果统一使用 WebSocketMessageSender 注入的 Bean。就不好区分各种情况,就会出现发送同步消息,存在循环发送消息的风险。
- Author:
- : gengwei.zheng
- Date:
- : 2023/10/26 23:26
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocketMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate, org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry) Creates an instance of aWebSocketMessagingTemplaterecord class. -
Method Summary
Modifier and TypeMethodDescriptionvoid发送 WebSocket 广播消息。发送全员信息final booleanIndicates whether some other object is "equal to" this one.org.springframework.messaging.simp.user.SimpUser根据用户 ID 获取到对应的 WebSocket 用户final inthashCode()Returns a hash code value for this object.booleanisUserExist(String userId) 判断 WebSocket用户是否存在。voidpointToPoint(String user, String destination, Object payload) 发送 WebSocket 点对点消息。发送信息给指定用户org.springframework.messaging.simp.SimpMessagingTemplateReturns the value of thesimpMessagingTemplaterecord component.org.springframework.messaging.simp.user.SimpUserRegistryReturns the value of thesimpUserRegistryrecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
WebSocketMessagingTemplate
public WebSocketMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate, org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry) Creates an instance of aWebSocketMessagingTemplaterecord class.- Parameters:
simpMessagingTemplate- the value for thesimpMessagingTemplaterecord componentsimpUserRegistry- the value for thesimpUserRegistryrecord component
-
-
Method Details
-
pointToPoint
发送 WebSocket 点对点消息。发送信息给指定用户- Parameters:
user- 用户唯一标识destination- 消息同奥payload- 消息内容
-
broadcast
发送 WebSocket 广播消息。发送全员信息- Parameters:
destination- 消息同奥payload- 消息内容
-
getUser
根据用户 ID 获取到对应的 WebSocket 用户- Parameters:
userId- 系统用户ID- Returns:
- WebSocket 用户
SimpUser
-
isUserExist
判断 WebSocket用户是否存在。注意:只能查询到当前所在 WebSocket实例中的实时 WebSocket 用户信息。如果实时用户在不同的实例中,则查询不到。
- Parameters:
userId- 用户ID- Returns:
- true 用户存在,false 用户不存在
-
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
simpMessagingTemplate
public org.springframework.messaging.simp.SimpMessagingTemplate simpMessagingTemplate()Returns the value of thesimpMessagingTemplaterecord component.- Returns:
- the value of the
simpMessagingTemplaterecord component
-
simpUserRegistry
public org.springframework.messaging.simp.user.SimpUserRegistry simpUserRegistry()Returns the value of thesimpUserRegistryrecord component.- Returns:
- the value of the
simpUserRegistryrecord component
-