From 1129cc278ec2f7f1fd3eb325b11974bd884346da Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期三, 09 四月 2025 13:48:49 +0800 Subject: [PATCH] 灌溉计划详情增加计划状态 --- pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java | 140 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 140 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..f2c0041 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java @@ -0,0 +1,140 @@ +package com.dy.pipIrrRemote.largeScreen; + +import lombok.extern.slf4j.Slf4j; +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/{id}") +@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 onLineDt ; + // 瀹㈡埛绔痠d + private String id = ""; + + /** + * 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉� + * @param session websocket浼氳瘽瀵硅薄 + * @param id 瀹㈡埛绔痠d + */ + @OnOpen + public void onOpen(Session session, @PathParam("id") String id) { + this.session = session; + //this.onLineDt = DateTime.yyyy_MM_dd_HH_mm_ss() ; + this.id = id; + if(this.id == null || this.id.length() == 0){ + this.id = "" + System.nanoTime() ; + } + 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 sendMessage2AllClient(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 sendMessage2OneClient(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--; + } +} -- Gitblit v1.8.0