From c937e665ccbb763968d30ef2fad61c9eb935abfe Mon Sep 17 00:00:00 2001
From: whycq <123456>
Date: 星期四, 02 五月 2024 10:29:43 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/phpswcs' into phpswcs

---
 src/main/java/com/zy/common/config/WebSocketConfig.java |   24 ++++++
 src/main/java/com/zy/common/model/WebSocketMessage.java |   14 +++
 src/main/java/com/zy/asrs/ws/ConsoleWebSocket.java      |  150 +++++++++++++++++++++++++++++++++++++
 pom.xml                                                 |    4 +
 4 files changed, 192 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index 354121d..4a2f68e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,6 +27,10 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+        </dependency>
         <!--<dependency>-->
             <!--<groupId>org.springframework.boot</groupId>-->
             <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
diff --git a/src/main/java/com/zy/asrs/ws/ConsoleWebSocket.java b/src/main/java/com/zy/asrs/ws/ConsoleWebSocket.java
new file mode 100644
index 0000000..dedc11b
--- /dev/null
+++ b/src/main/java/com/zy/asrs/ws/ConsoleWebSocket.java
@@ -0,0 +1,150 @@
+package com.zy.asrs.ws;
+
+import com.alibaba.fastjson.JSON;
+import com.zy.common.model.WebSocketMessage;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+import javax.websocket.*;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+@Component
+@Slf4j
+@Service
+@ServerEndpoint("/console/websocket")
+@Data
+public class ConsoleWebSocket {
+
+    //瀹㈡埛绔湪绾夸汉鏁�
+    private static int onlineClient = 0;
+
+    //瀹㈡埛绔睜
+    private static CopyOnWriteArraySet<ConsoleWebSocket> webSocketServers = new CopyOnWriteArraySet<>();
+
+    private Session session;
+
+    //鏄惁閴存潈锛岄粯璁ゆ湭閴存潈
+    private boolean auth = false;
+
+    //鏄惁涓虹鐞嗗憳
+    private boolean isAdmin = false;
+
+    //绠¢亾ID
+    private String sessionId;
+
+    @OnOpen
+    public void onOpen(Session session) {
+        this.session = session;
+        this.sessionId = session.getId();
+
+        //灏唚ebsocket瀵硅薄杩涜淇濆瓨
+        webSocketServers.add(this);
+        //娣诲姞鍦ㄧ嚎浜烘暟
+        addOnlineClient();
+        log.info("鏈夋柊绐楀彛寮�濮嬬洃鍚�:" + session.getId() + ",褰撳墠鍦ㄧ嚎浜烘暟涓�:" + getOnlineClient());
+    }
+
+    /**
+     * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
+     */
+    @OnClose
+    public void onClose() {
+        webSocketServers.remove(this);  //浠巗et涓垹闄�
+        subOnlineClient();           //鍦ㄧ嚎鏁板噺1
+        log.info("鍏抽棴鐨勮繛鎺ワ細" + sessionId);
+        log.info("鏈変竴杩炴帴鍏抽棴锛佸綋鍓嶅湪绾夸汉鏁颁负" + getOnlineClient());
+    }
+
+    /**
+     * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
+     * @ Param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+     */
+    @OnMessage
+    public void onMessage(String message, Session session) throws IOException {
+        WebSocketMessage socketMessage = JSON.parseObject(message, WebSocketMessage.class);
+        if (socketMessage.getUrl().equals("getTvConfig")) {
+            int tvNo = Integer.parseInt(socketMessage.getData());
+            if (tvNo == 1) {
+                HashMap<String, Object> map = new HashMap<>();
+                map.put("ip", "127.0.0.1");
+                map.put("port", "111");
+                socketMessage.setResult(JSON.toJSONString(map));
+                this.sendMessage(JSON.toJSONString(socketMessage));
+            }
+        }
+//        log.info("鏀跺埌鏉ヨ嚜杩炴帴锛�" + sessionId + "鐨勪俊鎭�:" + message);
+    }
+
+    /**
+     * @ Param session
+     * @ Param error
+     */
+    @OnError
+    public void onError(Session session, Throwable error) {
+        log.error("鍙戠敓閿欒");
+        error.printStackTrace();
+    }
+
+    /**
+     * 瀹炵幇鏈嶅姟鍣ㄤ富鍔ㄦ帹閫�
+     */
+    public void sendMessage(String message) throws IOException {
+        this.session.getBasicRemote().sendText(message);
+    }
+
+    /**
+     * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佺粰鎸囧畾鐢ㄦ埛
+     */
+    public void sendMessage(String message, String account) throws IOException {
+        for (ConsoleWebSocket item : webSocketServers) {
+            item.sendMessage(message);
+        }
+    }
+
+    public void sendMessage(String message, int userId) throws IOException {
+        for (ConsoleWebSocket item : webSocketServers) {
+            item.sendMessage(message);
+        }
+    }
+
+    /**
+     * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佺粰鎸囧畾鐢ㄦ埛
+     */
+    public static boolean sendMessageGlobal(String message, String account) throws IOException {
+        boolean tag = false;
+        for (ConsoleWebSocket item : webSocketServers) {
+            tag = true;
+            item.sendMessage(message);
+        }
+        return tag;
+    }
+
+    public static boolean sendMessageGlobal(String message, int userId) throws IOException {
+        boolean tag = false;
+        for (ConsoleWebSocket item : webSocketServers) {
+            tag = true;
+            item.sendMessage(message);
+        }
+        return tag;
+    }
+
+    public static synchronized int getOnlineClient() {
+        return onlineClient;
+    }
+
+    public static synchronized void addOnlineClient() {
+        ConsoleWebSocket.onlineClient++;
+    }
+
+    public static synchronized void subOnlineClient() {
+        if (ConsoleWebSocket.onlineClient > 0) {
+            ConsoleWebSocket.onlineClient--;
+        }
+    }
+
+}
diff --git a/src/main/java/com/zy/common/config/WebSocketConfig.java b/src/main/java/com/zy/common/config/WebSocketConfig.java
new file mode 100644
index 0000000..32c1bd6
--- /dev/null
+++ b/src/main/java/com/zy/common/config/WebSocketConfig.java
@@ -0,0 +1,24 @@
+package com.zy.common.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
+import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
+import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+@EnableWebSocketMessageBroker
+public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
+
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        return new ServerEndpointExporter();
+    }
+
+    @Override
+    public void registerStompEndpoints(StompEndpointRegistry registry) {
+        registry.addEndpoint("/api/socket");
+    }
+}
+
diff --git a/src/main/java/com/zy/common/model/WebSocketMessage.java b/src/main/java/com/zy/common/model/WebSocketMessage.java
new file mode 100644
index 0000000..d3c37ca
--- /dev/null
+++ b/src/main/java/com/zy/common/model/WebSocketMessage.java
@@ -0,0 +1,14 @@
+package com.zy.common.model;
+
+import lombok.Data;
+
+@Data
+public class WebSocketMessage {
+
+    private String url;
+
+    private String data;
+
+    private String result;
+
+}

--
Gitblit v1.9.1