pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webUtil/WebSocketMessage.java
New file @@ -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æ°æ® } pipIrr-platform/pipIrr-global/src/main/resources/application-global(166webÎļþ»¨Éú¿Ç).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» pipIrr-platform/pipIrr-global/src/main/resources/application-global(mj÷½ÏµÍ³).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» pipIrr-platform/pipIrr-global/src/main/resources/application-global(mqÃñÇÚϵͳ).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» pipIrr-platform/pipIrr-global/src/main/resources/application-global(spɳÅÌϵͳ).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» pipIrr-platform/pipIrr-global/src/main/resources/application-global(test²âÊÔϵͳ).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» pipIrr-platform/pipIrr-global/src/main/resources/application-global(ymԪıϵͳ).yml
@@ -348,6 +348,7 @@ - /sso/sso - /remote/comRes/receive - /remote/rtuUpgradeStateReceiver/receive - /remote/websocket - /wx/comRes/receive - /app/captcha/get - /file/file #webåå¸å¼æä»¶ç³»ç» 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åå¸å¼æä»¶ç³»ç» 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> 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ï¼æ¬æ¨¡åä¸å¯å¨websocket @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 { pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/config/WebSocketConfig.java
New file @@ -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; //å¼å¯WebSocketçæ¯æï¼å¹¶æè¯¥ç±»æ³¨å ¥å°spring容å¨ä¸ @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketHeartBeat.java
New file @@ -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) ; } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java
New file @@ -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 客æ·ç«¯ï¼æµè§å¨ï¼æ¯å»ºç«ä¸ä¸ªwebsocketè¿æ¥ï¼æå¡ç«¯å°±ä¼å建ä¸ä¸ªWebSocketServerå®ä¾ */ @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; // 客æ·ç«¯id private String id = ""; /** * è¿æ¥å»ºç«æåè°ç¨çæ¹æ³ * @param session websocketä¼è¯å¯¹è±¡ * @param id 客æ·ç«¯id */ @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); //ä»setä¸å é¤ WebSocketServer.subOnlineCount(); } log.info("客æ·ç«¯:" + id + "ï¼å ³éäºwebsocket"); } /** * æ¶å°å®¢æ·ç«¯æ¶æ¯åè°ç¨çæ¹æ³ * @param message 客æ·ç«¯åéè¿æ¥çæ¶æ¯ * @param session */ @OnMessage public void onMessage(String message, Session session) { log.info("客æ·ç«¯:" + id + "ï¼websocketæ¥æ:" + message); } /** * ä¼è¯å¼å¸¸ * @param session * @param error */ @OnError public void onError(Session session, Throwable error) { log.error("客æ·ç«¯:" + this.id + "ï¼websocketä¼è¯å¼å¸¸ï¼åå :" + error.getMessage()); } /** * æå¡å¨ä¸»å¨æ¨éæ¶æ¯ */ public void sendMessage(String message) { try{ this.session.getBasicRemote().sendText(message); }catch (Exception e){ log.error("客æ·ç«¯:" + id + "ï¼websocketç½ç»åéæ°æ®å¼å¸¸", 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--; } } 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> pipIrr-platform/pom.xml
@@ -27,6 +27,9 @@ </modules> <properties> <!-- ä¸java.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>