From 3a67a6599fb6565d4dad84ac86b597be4636ebee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Wed, 13 Sep 2023 12:56:47 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=20websocket=20?= =?UTF-8?q?=E7=BE=A4=E5=8F=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../holder/WebSocketSessionHolder.java | 5 +++++ .../listener/WebSocketTopicListener.java | 7 +++++- .../websocket/utils/WebSocketUtils.java | 22 ++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/holder/WebSocketSessionHolder.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/holder/WebSocketSessionHolder.java index 9b931938..de8c5a75 100644 --- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/holder/WebSocketSessionHolder.java +++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/holder/WebSocketSessionHolder.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import org.springframework.web.socket.WebSocketSession; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** @@ -31,6 +32,10 @@ public class WebSocketSessionHolder { return USER_SESSION_MAP.get(sessionKey); } + public static Set getSessionsAll() { + return USER_SESSION_MAP.keySet(); + } + public static Boolean existSession(Long sessionKey) { return USER_SESSION_MAP.containsKey(sessionKey); } diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/listener/WebSocketTopicListener.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/listener/WebSocketTopicListener.java index be385a7b..01528d0c 100644 --- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/listener/WebSocketTopicListener.java +++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/listener/WebSocketTopicListener.java @@ -19,13 +19,18 @@ public class WebSocketTopicListener implements ApplicationRunner, Ordered { @Override public void run(ApplicationArguments args) throws Exception { WebSocketUtils.subscribeMessage((message) -> { - log.info("WebSocket主题订阅收到消息session keys={} message={}!", message.getSessionKeys(), message.getMessage()); + log.info("WebSocket主题订阅收到消息session keys={} message={}", message.getSessionKeys(), message.getMessage()); + // 如果key不为空就按照key发消息 如果为空就群发 if (CollUtil.isNotEmpty(message.getSessionKeys())) { message.getSessionKeys().forEach(key -> { if (WebSocketSessionHolder.existSession(key)) { WebSocketUtils.sendMessage(key, message.getMessage()); } }); + } else { + WebSocketSessionHolder.getSessionsAll().forEach(key -> { + WebSocketUtils.sendMessage(key, message.getMessage()); + }); } }); log.info("初始化WebSocket主题订阅监听器成功"); diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java index c0bd2f3b..28679e4d 100644 --- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java +++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java @@ -1,13 +1,13 @@ package org.dromara.common.websocket.utils; import cn.hutool.core.collection.CollUtil; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.dromara.common.core.domain.model.LoginUser; import org.dromara.common.redis.utils.RedisUtils; import org.dromara.common.websocket.dto.WebSocketMessageDto; import org.dromara.common.websocket.holder.WebSocketSessionHolder; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.springframework.web.socket.PongMessage; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketMessage; @@ -77,6 +77,22 @@ public class WebSocketUtils { } } + /** + * 发布订阅的消息(群发) + * + * @param message 消息内容 + */ + public static void publishAll(String message) { + WebSocketSessionHolder.getSessionsAll().forEach(key -> { + WebSocketUtils.sendMessage(key, message); + }); + WebSocketMessageDto broadcastMessage = new WebSocketMessageDto(); + broadcastMessage.setMessage(message); + RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> { + log.info(" WebSocket发送主题订阅消息topic:{} message:{}", WEB_SOCKET_TOPIC, message); + }); + } + public static void sendPongMessage(WebSocketSession session) { sendMessage(session, new PongMessage()); }