From 62b4dfb917bb21ff704f84d6deecf24b044ef89a Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 22 七月 2025 18:10:40 +0800
Subject: [PATCH] 完善代码

---
 pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java |  165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 165 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java
new file mode 100644
index 0000000..e466d2e
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java
@@ -0,0 +1,165 @@
+package com.dy.pipIrrRemote.largeScreen;
+
+import com.dy.common.aop.SsoCheck;
+import com.dy.common.aop.SsoVo;
+import com.dy.common.springUtil.SpringContextUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+import jakarta.websocket.*;
+import jakarta.websocket.server.PathParam;
+import jakarta.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/2/10 15:25
+ * @Description 瀹㈡埛绔紙娴忚鍣級姣忓缓绔嬩竴涓獁ebsocket杩炴帴锛屾湇鍔$灏变細鍒涘缓涓�涓猈ebSocketServer瀹炰緥
+ * 搴旂敤鍓嶆彁鏄瓨鍦ㄤ竴涓狢onfig绫伙紝濡傛湰妯″潡鐨刉ebSocketConfig锛岃繖鏃禓ServerEndpoint鍙桽pringBoot瀹瑰櫒
+ * 绠$悊浜嗭紙鍦ㄥ祵鍏ュ紡web Servlet鐜涓級
+ */
+@Slf4j
+@ServerEndpoint("/websocket/ls/{token}")
+@Component
+@Scope("prototype") // 闈炲崟渚嬶紝姣忔璇锋眰閮戒細鍒涘缓鏂扮殑瀹炰緥
+public class WebSocketServer {
+
+    // 闈欐�佸彉閲忥紝璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
+    private static int onlineCount = 0;
+
+    // 瀛樻斁姣忎釜瀹㈡埛绔搴旂殑WebSocketServer瀵硅薄
+    private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
+
+    // 瀹㈡埛绔繛鎺ヤ細璇濓紝閫氳繃瀹冪粰瀹㈡埛绔彂閫佹暟鎹�
+    private Session session;
+
+    private String orgTag;
+
+    // 瀹㈡埛绔笂绾挎椂鍒�
+    //private String onLineDt ;
+    // 瀹㈡埛绔�
+    private String token = "";
+
+    @Autowired
+    private SsoCheck ssoCheck ;
+
+    /**
+     * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
+     * @param session websocket浼氳瘽瀵硅薄
+     * @param token 瀹㈡埛绔痠d
+     */
+    @OnOpen
+    public void onOpen(Session session, @PathParam("token") String token) {
+        if(this.token == null || this.token.length() == 0){
+            this.session = session;
+            //this.onLineDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;
+            this.token = token;
+            this.sendMessage(WebSocketHeartBeat.getHeartBeatMessage());
+
+            if (webSocketMap.containsKey(token)) {
+                webSocketMap.remove(token);
+                webSocketMap.put(token, this);
+            } else {
+                webSocketMap.put(token, this);
+                WebSocketServer.addOnlineCount();
+            }
+            if(this.ssoCheck == null){
+                //2025-07-22 涓嶇煡涓轰粈涔堬紝this.ssoCheck浼氫负null
+                this.ssoCheck = SpringContextUtil.getBean(SsoCheck.class);
+            }
+            Object rObj = this.ssoCheck.check(token);
+            if(rObj != null) {
+                if (rObj instanceof SsoVo ssoVo) {
+                    this.orgTag = ssoVo.dataSourceName ;
+                }
+            }
+        }
+    }
+
+    /**
+     * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
+     */
+    @OnClose
+    public void onClose() {
+        if (webSocketMap.containsKey(token)) {
+            webSocketMap.remove(token);
+            //浠巗et涓垹闄�
+            WebSocketServer.subOnlineCount();
+        }
+        log.info("瀹㈡埛绔�:" + token + "锛屽叧闂簡websocket");
+    }
+
+    /**
+     * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
+     * @param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+     * @param session
+     */
+    @OnMessage
+    public void onMessage(String message, Session session) {
+        log.info("瀹㈡埛绔�:" + token + "锛寃ebsocket鎶ユ枃:" + message);
+    }
+
+    /**
+     * 浼氳瘽寮傚父
+     * @param session
+     * @param error
+     */
+    @OnError
+    public void onError(Session session, Throwable error) {
+        log.error("瀹㈡埛绔�:" + this.token + "锛寃ebsocket浼氳瘽寮傚父锛屽師鍥�:" + error.getMessage());
+    }
+
+    /**
+     * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佹秷鎭�
+     */
+    public void sendMessage(String message) {
+        try{
+            this.session.getBasicRemote().sendText(message);
+        }catch (Exception e){
+            log.error("瀹㈡埛绔�:" + token + "锛寃ebsocket缃戠粶鍙戦�佹暟鎹紓甯�", e);
+        }
+    }
+
+    /**
+     * 鏈嶅姟鍣ㄤ富鍔ㄧ兢鎺ㄩ�佹秷鎭�
+     */
+    public static void sendMessage2AllClient(String orgTag, String message) throws IOException {
+        ConcurrentHashMap.KeySetView<String, WebSocketServer> tokens = webSocketMap.keySet();
+        for (String token : tokens) {
+            WebSocketServer webSocketServer = webSocketMap.get(token);
+            if(orgTag == null){
+                webSocketServer.sendMessage(message);
+            }else{
+                if(webSocketServer.orgTag != null && webSocketServer.orgTag.equals(orgTag)){
+                    webSocketServer.sendMessage(message);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鏈嶅姟鍣ㄦ寚瀹氬鎴风鎺ㄩ�佹秷鎭�
+     */
+    public static void sendMessage2OneClient(String message, String token) throws IOException {
+        if (message != null && message.length() != 0 && webSocketMap.containsKey(token)) {
+            webSocketMap.get(token).sendMessage(message);
+        } else {
+            log.error("瀹㈡埛绔�" + token + "锛屼笉鍦ㄧ嚎锛�");
+        }
+    }
+
+    public static synchronized int getOnlineCount() {
+        return onlineCount;
+    }
+
+    public static synchronized void addOnlineCount() {
+        WebSocketServer.onlineCount++;
+    }
+
+    public static synchronized void subOnlineCount() {
+        WebSocketServer.onlineCount--;
+    }
+}

--
Gitblit v1.8.0