From 7b119de9ee015934f8b001b6038e282522993c80 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 11 二月 2025 11:55:40 +0800
Subject: [PATCH] 远程模块实现websocket基本框架
---
pipIrr-platform/pom.xml | 11 ++
pipIrr-platform/pipIrr-global/src/main/resources/application-global(test测试系统).yml | 1
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebSocketConfig.java | 19 +++
pipIrr-platform/pipIrr-global/src/main/resources/application-global(mq民勤系统).yml | 1
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/PipIrrRemoteApplication.java | 8 +
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java | 131 ++++++++++++++++++++++++++
pipIrr-platform/pipIrr-global/src/main/resources/application-global(sp沙盘系统).yml | 1
pipIrr-platform/pipIrr-web/pom.xml | 5 +
pipIrr-platform/pipIrr-global/src/main/resources/application-global(166web文件花生壳).yml | 1
pipIrr-platform/pipIrr-global/src/main/resources/application-global(ym元谋系统).yml | 1
pipIrr-platform/pipIrr-web/pipIrr-web-remote/pom.xml | 7 -
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webUtil/WebSocketMessage.java | 33 ++++++
pipIrr-platform/pipIrr-global/src/main/resources/application-global(mj梅江系统).yml | 1
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketHeartBeat.java | 32 ++++++
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml | 1
15 files changed, 245 insertions(+), 8 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webUtil/WebSocketMessage.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webUtil/WebSocketMessage.java
new file mode 100644
index 0000000..87d8cb5
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webUtil/WebSocketMessage.java
@@ -0,0 +1,33 @@
+package com.dy.common.webUtil;
+
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/2/11 10:41
+ * @Description
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+@Schema(name="websocket娑堟伅鍩虹被")
+@JsonPropertyOrder({"type", "code", "content"})
+public class WebSocketMessage<T> {
+
+ @Schema(description = "鏁版嵁绫诲瀷")
+ public String type;
+ @Schema(description = "鏁版嵁缂栫爜")
+ public String code;//鏁版嵁缂栫爜锛屽叿浣撴暟鎹叿浣撳畾涔夊叾缂栫爜锛屽彲浠ヤ负绌�
+ @Schema(description = "鏁版嵁")
+ public T content;
+
+ public static final String TYPE_HEARTBEAT = "HEARTBEAT";//蹇冭烦锛屼篃鏄枃鏈暟鎹紝鏁版嵁鍐呭鏄湇鍔$鏃跺埢
+ public static final String TYPE_TEXT = "TEXT";//鏂囨湰鏁版嵁
+ public static final String TYPE_JSON = "JSON";//json鏁版嵁
+}
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050166web\346\226\207\344\273\266\350\212\261\347\224\237\345\243\263\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050166web\346\226\207\344\273\266\350\212\261\347\224\237\345\243\263\051.yml"
index 0473a51..3d97a05 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050166web\346\226\207\344\273\266\350\212\261\347\224\237\345\243\263\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050166web\346\226\207\344\273\266\350\212\261\347\224\237\345\243\263\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mj\346\242\205\346\261\237\347\263\273\347\273\237\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mj\346\242\205\346\261\237\347\263\273\347\273\237\051.yml"
index ac591e0..d43eb8e 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mj\346\242\205\346\261\237\347\263\273\347\273\237\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mj\346\242\205\346\261\237\347\263\273\347\273\237\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mq\346\260\221\345\213\244\347\263\273\347\273\237\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mq\346\260\221\345\213\244\347\263\273\347\273\237\051.yml"
index 7a2ebe1..a8a77a0 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mq\346\260\221\345\213\244\347\263\273\347\273\237\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050mq\346\260\221\345\213\244\347\263\273\347\273\237\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050sp\346\262\231\347\233\230\347\263\273\347\273\237\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050sp\346\262\231\347\233\230\347\263\273\347\273\237\051.yml"
index 85ceb5f..93de14e 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050sp\346\262\231\347\233\230\347\263\273\347\273\237\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050sp\346\262\231\347\233\230\347\263\273\347\273\237\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050test\346\265\213\350\257\225\347\263\273\347\273\237\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050test\346\265\213\350\257\225\347\263\273\347\273\237\051.yml"
index acb24ce..cdaa6e0 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050test\346\265\213\350\257\225\347\263\273\347\273\237\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050test\346\265\213\350\257\225\347\263\273\347\273\237\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050ym\345\205\203\350\260\213\347\263\273\347\273\237\051.yml" "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050ym\345\205\203\350\260\213\347\263\273\347\273\237\051.yml"
index 345cf58..c1cf48a 100644
--- "a/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050ym\345\205\203\350\260\213\347\263\273\347\273\237\051.yml"
+++ "b/pipIrr-platform/pipIrr-global/src/main/resources/application-global\050ym\345\205\203\350\260\213\347\263\273\347\273\237\051.yml"
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git a/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml b/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
index 4be4239..f1bcac3 100644
--- a/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
+++ b/pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -348,6 +348,7 @@
- /sso/sso
- /remote/comRes/receive
- /remote/rtuUpgradeStateReceiver/receive
+ - /remote/websocket
- /wx/comRes/receive
- /app/captcha/get
- /file/file #web鍒嗗竷寮忔枃浠剁郴缁�
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/pom.xml b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/pom.xml
index 4bd9060..728d920 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/pom.xml
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/pom.xml
@@ -9,12 +9,7 @@
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>com.dy</groupId>
- <artifactId>pipIrr-web-sell</artifactId>
- <version>1.0.0</version>
- <scope>compile</scope>
- </dependency>
+
</dependencies>
<parent>
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/PipIrrRemoteApplication.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/PipIrrRemoteApplication.java
index 0dfe832..e0c3ded 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/PipIrrRemoteApplication.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/PipIrrRemoteApplication.java
@@ -8,7 +8,12 @@
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
+import org.springframework.boot.web.servlet.ServletComponentScan;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
@SpringBootApplication
+@EnableScheduling //鍚姩瀹氭椂浠诲姟锛屾湰妯″潡websocket鍙戦�佸績璺�
+@ServletComponentScan //鎵弿servlet锛屾湰妯″潡涓惎鍔╳ebsocket
@EnableAspectJAutoProxy
@EnableMultiDataSource
@ComponentScan(basePackages = {"com.dy.common", "com.dy.pipIrrGlobal", "com.dy.pipIrrRemote"},
@@ -23,7 +28,8 @@
"com.dy.pipIrrGlobal.daoSe",
"com.dy.pipIrrGlobal.daoBa",
"com.dy.pipIrrGlobal.daoFi",
- "com.dy.pipIrrGlobal.daoAllRound"
+ "com.dy.pipIrrGlobal.daoAllRound",
+ "com.dy.pipIrrGlobal.daoLargeScreen"
})
public class PipIrrRemoteApplication {
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebSocketConfig.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebSocketConfig.java
new file mode 100644
index 0000000..7d65a4b
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebSocketConfig.java
@@ -0,0 +1,19 @@
+package com.dy.pipIrrRemote.config;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/2/10 15:42
+ * @Description
+ */
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+//寮�鍚疻ebSocket鐨勬敮鎸侊紝骞舵妸璇ョ被娉ㄥ叆鍒皊pring瀹瑰櫒涓�
+@Configuration
+public class WebSocketConfig {
+ @Bean
+ public ServerEndpointExporter serverEndpointExporter() {
+ return new ServerEndpointExporter();
+ }
+}
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketHeartBeat.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketHeartBeat.java
new file mode 100644
index 0000000..b122c31
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketHeartBeat.java
@@ -0,0 +1,32 @@
+package com.dy.pipIrrRemote.largeScreen;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/2/11 8:37
+ * @Description
+ */
+
+import com.alibaba.fastjson2.JSON;
+import com.dy.common.util.DateTime;
+import com.dy.common.webUtil.WebSocketMessage;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+@Component
+public class WebSocketHeartBeat {
+
+ // 璁剧疆瀹氭椂鍗佺涓�娆�
+ @Scheduled(cron = "0/10 * * * * ?")
+ public void WsHeartBeat() throws Exception {
+ WebSocketServer.sendAllMessage(getHeartBeatMessage());
+ }
+
+ public static String getHeartBeatMessage() {
+ WebSocketMessage vo = new WebSocketMessage() ;
+ vo.type = WebSocketMessage.TYPE_HEARTBEAT ;
+ vo.code = "" ;//蹇冭烦鏁版嵁缂栫爜灏辨槸绌哄瓧绗︿覆
+ vo.content = DateTime.yyyy_MM_dd_HH_mm_ss() ;
+ return JSON.toJSONString(vo) ;
+ }
+
+}
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..9bcf52c
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java
@@ -0,0 +1,131 @@
+package com.dy.pipIrrRemote.largeScreen;
+
+import lombok.extern.slf4j.Slf4j;
+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瀹炰緥
+ */
+@Slf4j
+@ServerEndpoint("/websocket/ls/{id}")
+@Component
+public class WebSocketServer {
+
+ // 闈欐�佸彉閲忥紝璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
+ private static int onlineCount = 0;
+
+ // 瀛樻斁姣忎釜瀹㈡埛绔搴旂殑WebSocketServer瀵硅薄
+ private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
+
+ // 瀹㈡埛绔繛鎺ヤ細璇濓紝閫氳繃瀹冪粰瀹㈡埛绔彂閫佹暟鎹�
+ private Session session;
+
+ // 瀹㈡埛绔痠d
+ private String id = "";
+
+ /**
+ * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
+ * @param session websocket浼氳瘽瀵硅薄
+ * @param id 瀹㈡埛绔痠d
+ */
+ @OnOpen
+ public void onOpen(Session session, @PathParam("id") String id) {
+ this.session = session;
+ this.id = id;
+ this.sendMessage(WebSocketHeartBeat.getHeartBeatMessage());
+
+ if (webSocketMap.containsKey(id)) {
+ webSocketMap.remove(id);
+ webSocketMap.put(id, this);
+ } else {
+ webSocketMap.put(id, this);
+ WebSocketServer.addOnlineCount();
+ }
+ }
+
+ /**
+ * 杩炴帴鍏抽棴璋冪敤鐨勬柟娉�
+ */
+ @OnClose
+ public void onClose() {
+ if (webSocketMap.containsKey(id)) {
+ webSocketMap.remove(id);
+ //浠巗et涓垹闄�
+ WebSocketServer.subOnlineCount();
+ }
+ log.info("瀹㈡埛绔�:" + id + "锛屽叧闂簡websocket");
+ }
+
+ /**
+ * 鏀跺埌瀹㈡埛绔秷鎭悗璋冪敤鐨勬柟娉�
+ * @param message 瀹㈡埛绔彂閫佽繃鏉ョ殑娑堟伅
+ * @param session
+ */
+ @OnMessage
+ public void onMessage(String message, Session session) {
+ log.info("瀹㈡埛绔�:" + id + "锛寃ebsocket鎶ユ枃:" + message);
+ }
+
+ /**
+ * 浼氳瘽寮傚父
+ * @param session
+ * @param error
+ */
+ @OnError
+ public void onError(Session session, Throwable error) {
+ log.error("瀹㈡埛绔�:" + this.id + "锛寃ebsocket浼氳瘽寮傚父锛屽師鍥�:" + error.getMessage());
+ }
+
+ /**
+ * 鏈嶅姟鍣ㄤ富鍔ㄦ帹閫佹秷鎭�
+ */
+ public void sendMessage(String message) {
+ try{
+ this.session.getBasicRemote().sendText(message);
+ }catch (Exception e){
+ log.error("瀹㈡埛绔�:" + id + "锛寃ebsocket缃戠粶鍙戦�佹暟鎹紓甯�", e);
+ }
+ }
+
+ /**
+ * 鏈嶅姟鍣ㄤ富鍔ㄧ兢鎺ㄩ�佹秷鎭�
+ */
+ public static void sendAllMessage(String message) throws IOException {
+ ConcurrentHashMap.KeySetView<String, WebSocketServer> ids = webSocketMap.keySet();
+ for (String id : ids) {
+ WebSocketServer webSocketServer = webSocketMap.get(id);
+ webSocketServer.sendMessage(message);
+ }
+ }
+
+ /**
+ * 鏈嶅姟鍣ㄦ寚瀹氬鎴风鎺ㄩ�佹秷鎭�
+ */
+ public static void sendOneMessage(String message, String id) throws IOException {
+ if (message != null && message.length() != 0 && webSocketMap.containsKey(id)) {
+ webSocketMap.get(id).sendMessage(message);
+ } else {
+ log.error("瀹㈡埛绔�" + id + "锛屼笉鍦ㄧ嚎锛�");
+ }
+ }
+
+ public static synchronized int getOnlineCount() {
+ return onlineCount;
+ }
+
+ public static synchronized void addOnlineCount() {
+ WebSocketServer.onlineCount++;
+ }
+
+ public static synchronized void subOnlineCount() {
+ WebSocketServer.onlineCount--;
+ }
+}
diff --git a/pipIrr-platform/pipIrr-web/pom.xml b/pipIrr-platform/pipIrr-web/pom.xml
index 4998d97..077869b 100644
--- a/pipIrr-platform/pipIrr-web/pom.xml
+++ b/pipIrr-platform/pipIrr-web/pom.xml
@@ -76,6 +76,11 @@
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-websocket</artifactId>
+ </dependency>
+
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
diff --git a/pipIrr-platform/pom.xml b/pipIrr-platform/pom.xml
index 97dc779..0f86264 100644
--- a/pipIrr-platform/pom.xml
+++ b/pipIrr-platform/pom.xml
@@ -27,6 +27,9 @@
</modules>
<properties>
+ <!-- 涓巎ava.version閰嶇疆鍦ㄤ竴璧凤紝璁剧疆缂栫爜闆�-->
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>20</java.version>
<tomcat.version>10.1.12</tomcat.version>
<spring.boot.version>3.1.3</spring.boot.version>
@@ -66,7 +69,6 @@
<scope>import</scope>
</dependency>
- <!-- 杈撳叆鍙傛暟鎹獙璇� -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
@@ -89,6 +91,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-websocket</artifactId>
+ <version>3.1.3</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
<!-- lombok -->
<dependency>
--
Gitblit v1.8.0