From 102003a52b101c85d7e378e25f35ec6488620fc1 Mon Sep 17 00:00:00 2001
From: AprilWind <2100166581@qq.com>
Date: 星期一, 27 五月 2024 12:23:28 +0800
Subject: [PATCH] update 完善websocket包注释
---
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/config/WebSocketConfig.java | 7 +
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java | 47 +++++++++------
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/utils/WebSocketUtils.java | 35 ++++++++---
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/listener/WebSocketTopicListener.java | 9 ++
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/holder/WebSocketSessionHolder.java | 28 +++++++++
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/constant/WebSocketConstants.java | 1
ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/interceptor/PlusWebSocketInterceptor.java | 27 ++++----
7 files changed, 110 insertions(+), 44 deletions(-)
diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/config/WebSocketConfig.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/config/WebSocketConfig.java
index 30d109e..ef5cfc9 100644
--- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/config/WebSocketConfig.java
+++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/config/WebSocketConfig.java
@@ -27,17 +27,20 @@
@Bean
public WebSocketConfigurer webSocketConfigurer(HandshakeInterceptor handshakeInterceptor,
- WebSocketHandler webSocketHandler,
- WebSocketProperties webSocketProperties) {
+ WebSocketHandler webSocketHandler, WebSocketProperties webSocketProperties) {
+ // 濡傛灉WebSocket鐨勮矾寰勪负绌猴紝鍒欒缃粯璁よ矾寰勪负 "/websocket"
if (StrUtil.isBlank(webSocketProperties.getPath())) {
webSocketProperties.setPath("/websocket");
}
+ // 濡傛灉鍏佽璺ㄥ煙璁块棶鐨勫湴鍧�涓虹┖锛屽垯璁剧疆涓� "*"锛岃〃绀哄厑璁告墍鏈夋潵婧愮殑璺ㄥ煙璇锋眰
if (StrUtil.isBlank(webSocketProperties.getAllowedOrigins())) {
webSocketProperties.setAllowedOrigins("*");
}
+ // 杩斿洖涓�涓猈ebSocketConfigurer瀵硅薄锛岀敤浜庨厤缃甒ebSocket
return registry -> registry
+ // 娣诲姞WebSocket澶勭悊绋嬪簭鍜屾嫤鎴櫒鍒版寚瀹氳矾寰勶紝璁剧疆鍏佽鐨勮法鍩熸潵婧�
.addHandler(webSocketHandler, webSocketProperties.getPath())
.addInterceptors(handshakeInterceptor)
.setAllowedOrigins(webSocketProperties.getAllowedOrigins());
diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/constant/WebSocketConstants.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/constant/WebSocketConstants.java
index 54eb447..e243279 100644
--- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/constant/WebSocketConstants.java
+++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/constant/WebSocketConstants.java
@@ -6,6 +6,7 @@
* @author zendwang
*/
public interface WebSocketConstants {
+
/**
* websocketSession涓殑鍙傛暟鐨刱ey
*/
diff --git a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java
index a53583f..759aece 100644
--- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java
+++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/handler/PlusWebSocketHandler.java
@@ -31,33 +31,42 @@
}
/**
- * 澶勭悊鍙戦�佹潵鐨勬枃鏈秷鎭�
+ * 澶勭悊鎺ユ敹鍒扮殑鏂囨湰娑堟伅
*
- * @param session
- * @param message
- * @throws Exception
+ * @param session WebSocket浼氳瘽
+ * @param message 鎺ユ敹鍒扮殑鏂囨湰娑堟伅
+ * @throws Exception 澶勭悊娑堟伅杩囩▼涓彲鑳芥姏鍑虹殑寮傚父
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
+ // 浠嶹ebSocket浼氳瘽涓幏鍙栫櫥褰曠敤鎴蜂俊鎭�
LoginUser loginUser = (LoginUser) session.getAttributes().get(LOGIN_USER_KEY);
- List<Long> userIds = List.of(loginUser.getUserId());
+
+ // 鍒涘缓WebSocket娑堟伅DTO瀵硅薄
WebSocketMessageDto webSocketMessageDto = new WebSocketMessageDto();
- webSocketMessageDto.setSessionKeys(userIds);
+ webSocketMessageDto.setSessionKeys(List.of(loginUser.getUserId()));
webSocketMessageDto.setMessage(message.getPayload());
WebSocketUtils.publishMessage(webSocketMessageDto);
}
+ /**
+ * 澶勭悊鎺ユ敹鍒扮殑浜岃繘鍒舵秷鎭�
+ *
+ * @param session WebSocket浼氳瘽
+ * @param message 鎺ユ敹鍒扮殑浜岃繘鍒舵秷鎭�
+ * @throws Exception 澶勭悊娑堟伅杩囩▼涓彲鑳芥姏鍑虹殑寮傚父
+ */
@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
super.handleBinaryMessage(session, message);
}
/**
- * 蹇冭烦鐩戞祴鐨勫洖澶�
+ * 澶勭悊鎺ユ敹鍒扮殑Pong娑堟伅锛堝績璺崇洃娴嬶級
*
- * @param session
- * @param message
- * @throws Exception
+ * @param session WebSocket浼氳瘽
+ * @param message 鎺ユ敹鍒扮殑Pong娑堟伅
+ * @throws Exception 澶勭悊娑堟伅杩囩▼涓彲鑳芥姏鍑虹殑寮傚父
*/
@Override
protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
@@ -65,11 +74,11 @@
}
/**
- * 杩炴帴鍑洪敊鏃�
+ * 澶勭悊WebSocket浼犺緭閿欒
*
- * @param session
- * @param exception
- * @throws Exception
+ * @param session WebSocket浼氳瘽
+ * @param exception 鍙戠敓鐨勫紓甯�
+ * @throws Exception 澶勭悊杩囩▼涓彲鑳芥姏鍑虹殑寮傚父
*/
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
@@ -77,10 +86,10 @@
}
/**
- * 杩炴帴鍏抽棴鍚�
+ * 鍦╓ebSocket杩炴帴鍏抽棴鍚庢墽琛屾竻鐞嗘搷浣�
*
- * @param session
- * @param status
+ * @param session WebSocket浼氳瘽
+ * @param status 鍏抽棴鐘舵�佷俊鎭�
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
@@ -90,9 +99,9 @@
}
/**
- * 鏄惁鏀寔鍒嗙墖娑堟伅
+ * 鎸囩ず澶勭悊绋嬪簭鏄惁鏀寔鎺ユ敹閮ㄥ垎娑堟伅
*
- * @return
+ * @return 濡傛灉鏀寔鎺ユ敹閮ㄥ垎娑堟伅锛屽垯杩斿洖true锛涘惁鍒欒繑鍥瀎alse
*/
@Override
public boolean supportsPartialMessages() {
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 de8c5a7..368801c 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
@@ -18,24 +18,52 @@
private static final Map<Long, WebSocketSession> USER_SESSION_MAP = new ConcurrentHashMap<>();
+ /**
+ * 灏哤ebSocket浼氳瘽娣诲姞鍒扮敤鎴蜂細璇滿ap涓�
+ *
+ * @param sessionKey 浼氳瘽閿紝鐢ㄤ簬妫�绱細璇�
+ * @param session 瑕佹坊鍔犵殑WebSocket浼氳瘽
+ */
public static void addSession(Long sessionKey, WebSocketSession session) {
USER_SESSION_MAP.put(sessionKey, session);
}
+ /**
+ * 浠庣敤鎴蜂細璇滿ap涓Щ闄ゆ寚瀹氫細璇濋敭瀵瑰簲鐨刉ebSocket浼氳瘽
+ *
+ * @param sessionKey 瑕佺Щ闄ょ殑浼氳瘽閿�
+ */
public static void removeSession(Long sessionKey) {
if (USER_SESSION_MAP.containsKey(sessionKey)) {
USER_SESSION_MAP.remove(sessionKey);
}
}
+ /**
+ * 鏍规嵁浼氳瘽閿粠鐢ㄦ埛浼氳瘽Map涓幏鍙朩ebSocket浼氳瘽
+ *
+ * @param sessionKey 瑕佽幏鍙栫殑浼氳瘽閿�
+ * @return 涓庣粰瀹氫細璇濋敭瀵瑰簲鐨刉ebSocket浼氳瘽锛屽鏋滀笉瀛樺湪鍒欒繑鍥瀗ull
+ */
public static WebSocketSession getSessions(Long sessionKey) {
return USER_SESSION_MAP.get(sessionKey);
}
+ /**
+ * 鑾峰彇瀛樺偍鍦ㄧ敤鎴蜂細璇滿ap涓墍鏈塛ebSocket浼氳瘽鐨勪細璇濋敭闆嗗悎
+ *
+ * @return 鎵�鏈塛ebSocket浼氳瘽鐨勪細璇濋敭闆嗗悎
+ */
public static Set<Long> getSessionsAll() {
return USER_SESSION_MAP.keySet();
}
+ /**
+ * 妫�鏌ョ粰瀹氱殑浼氳瘽閿槸鍚﹀瓨鍦ㄤ簬鐢ㄦ埛浼氳瘽Map涓�
+ *
+ * @param sessionKey 瑕佹鏌ョ殑浼氳瘽閿�
+ * @return 濡傛灉瀛樺湪瀵瑰簲鐨勪細璇濋敭锛屽垯杩斿洖true锛涘惁鍒欒繑鍥瀎alse
+ */
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/interceptor/PlusWebSocketInterceptor.java b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/interceptor/PlusWebSocketInterceptor.java
index 1dc4120..0abbbd3 100644
--- a/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/interceptor/PlusWebSocketInterceptor.java
+++ b/ruoyi-common/ruoyi-common-websocket/src/main/java/org/dromara/common/websocket/interceptor/PlusWebSocketInterceptor.java
@@ -1,8 +1,8 @@
package org.dromara.common.websocket.interceptor;
+import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
-import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
@@ -21,13 +21,13 @@
public class PlusWebSocketInterceptor implements HandshakeInterceptor {
/**
- * 鎻℃墜鍓�
+ * WebSocket鎻℃墜涔嬪墠鎵ц鐨勫墠缃鐞嗘柟娉�
*
- * @param request request
- * @param response response
- * @param wsHandler wsHandler
- * @param attributes attributes
- * @return 鏄惁鎻℃墜鎴愬姛
+ * @param request WebSocket鎻℃墜璇锋眰
+ * @param response WebSocket鎻℃墜鍝嶅簲
+ * @param wsHandler WebSocket澶勭悊绋嬪簭
+ * @param attributes 涓嶹ebSocket浼氳瘽鍏宠仈鐨勫睘鎬�
+ * @return 濡傛灉鍏佽鎻℃墜缁х画杩涜锛屽垯杩斿洖true锛涘惁鍒欒繑鍥瀎alse
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) {
@@ -37,15 +37,16 @@
}
/**
- * 鎻℃墜鍚�
+ * WebSocket鎻℃墜鎴愬姛鍚庢墽琛岀殑鍚庣疆澶勭悊鏂规硶
*
- * @param request request
- * @param response response
- * @param wsHandler wsHandler
- * @param exception 寮傚父
+ * @param request WebSocket鎻℃墜璇锋眰
+ * @param response WebSocket鎻℃墜鍝嶅簲
+ * @param wsHandler WebSocket澶勭悊绋嬪簭
+ * @param exception 鎻℃墜杩囩▼涓彲鑳藉嚭鐜扮殑寮傚父
*/
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
-
+ // 鍦ㄨ繖涓柟娉曚腑鍙互鎵ц涓�浜涙彙鎵嬫垚鍔熷悗鐨勫悗缁鐞嗛�昏緫锛屾瘮濡傝褰曟棩蹇楁垨鑰呭叾浠栨搷浣�
}
+
}
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 01528d0..0ad39af 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
@@ -1,9 +1,9 @@
package org.dromara.common.websocket.listener;
import cn.hutool.core.collection.CollUtil;
+import lombok.extern.slf4j.Slf4j;
import org.dromara.common.websocket.holder.WebSocketSessionHolder;
import org.dromara.common.websocket.utils.WebSocketUtils;
-import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
@@ -16,8 +16,15 @@
@Slf4j
public class WebSocketTopicListener implements ApplicationRunner, Ordered {
+ /**
+ * 鍦⊿pring Boot搴旂敤绋嬪簭鍚姩鏃跺垵濮嬪寲WebSocket涓婚璁㈤槄鐩戝惉鍣�
+ *
+ * @param args 搴旂敤绋嬪簭鍙傛暟
+ * @throws Exception 鍒濆鍖栬繃绋嬩腑鍙兘鎶涘嚭鐨勫紓甯�
+ */
@Override
public void run(ApplicationArguments args) throws Exception {
+ // 璁㈤槄WebSocket娑堟伅
WebSocketUtils.subscribeMessage((message) -> {
log.info("WebSocket涓婚璁㈤槄鏀跺埌娑堟伅session keys={} message={}", message.getSessionKeys(), message.getMessage());
// 濡傛灉key涓嶄负绌哄氨鎸夌収key鍙戞秷鎭� 濡傛灉涓虹┖灏辩兢鍙�
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 086599c..afe76e0 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
@@ -29,10 +29,10 @@
public class WebSocketUtils {
/**
- * 鍙戦�佹秷鎭�
+ * 鍚戞寚瀹氱殑WebSocket浼氳瘽鍙戦�佹秷鎭�
*
- * @param sessionKey session涓婚敭 涓�鑸负鐢ㄦ埛id
- * @param message 娑堟伅鏂囨湰
+ * @param sessionKey 瑕佸彂閫佹秷鎭殑鐢ㄦ埛id
+ * @param message 瑕佸彂閫佺殑娑堟伅鍐呭
*/
public static void sendMessage(Long sessionKey, String message) {
WebSocketSession session = WebSocketSessionHolder.getSessions(sessionKey);
@@ -40,18 +40,18 @@
}
/**
- * 璁㈤槄娑堟伅
+ * 璁㈤槄WebSocket娑堟伅涓婚锛屽苟鎻愪緵涓�涓秷璐硅�呭嚱鏁版潵澶勭悊鎺ユ敹鍒扮殑娑堟伅
*
- * @param consumer 鑷畾涔夊鐞�
+ * @param consumer 澶勭悊WebSocket娑堟伅鐨勬秷璐硅�呭嚱鏁�
*/
public static void subscribeMessage(Consumer<WebSocketMessageDto> consumer) {
RedisUtils.subscribe(WEB_SOCKET_TOPIC, WebSocketMessageDto.class, consumer);
}
/**
- * 鍙戝竷璁㈤槄鐨勬秷鎭�
+ * 鍙戝竷WebSocket璁㈤槄娑堟伅
*
- * @param webSocketMessage 娑堟伅瀵硅薄
+ * @param webSocketMessage 瑕佸彂甯冪殑WebSocket娑堟伅瀵硅薄
*/
public static void publishMessage(WebSocketMessageDto webSocketMessage) {
List<Long> unsentSessionKeys = new ArrayList<>();
@@ -76,9 +76,9 @@
}
/**
- * 鍙戝竷璁㈤槄鐨勬秷鎭�(缇ゅ彂)
+ * 鍚戞墍鏈夌殑WebSocket浼氳瘽鍙戝竷璁㈤槄鐨勬秷鎭�(缇ゅ彂)
*
- * @param message 娑堟伅鍐呭
+ * @param message 瑕佸彂甯冪殑娑堟伅鍐呭
*/
public static void publishAll(String message) {
WebSocketMessageDto broadcastMessage = new WebSocketMessageDto();
@@ -88,14 +88,31 @@
});
}
+ /**
+ * 鍚戞寚瀹氱殑WebSocket浼氳瘽鍙戦�丳ong娑堟伅
+ *
+ * @param session 瑕佸彂閫丳ong娑堟伅鐨刉ebSocket浼氳瘽
+ */
public static void sendPongMessage(WebSocketSession session) {
sendMessage(session, new PongMessage());
}
+ /**
+ * 鍚戞寚瀹氱殑WebSocket浼氳瘽鍙戦�佹枃鏈秷鎭�
+ *
+ * @param session WebSocket浼氳瘽
+ * @param message 瑕佸彂閫佺殑鏂囨湰娑堟伅鍐呭
+ */
public static void sendMessage(WebSocketSession session, String message) {
sendMessage(session, new TextMessage(message));
}
+ /**
+ * 鍚戞寚瀹氱殑WebSocket浼氳瘽鍙戦�乄ebSocket娑堟伅瀵硅薄
+ *
+ * @param session WebSocket浼氳瘽
+ * @param message 瑕佸彂閫佺殑WebSocket娑堟伅瀵硅薄
+ */
private static void sendMessage(WebSocketSession session, WebSocketMessage<?> message) {
if (session == null || !session.isOpen()) {
log.warn("[send] session浼氳瘽宸茬粡鍏抽棴");
--
Gitblit v1.9.3