From 7d55b601b8ec846e9d48ce31de1c5c6930d6dee0 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 19 八月 2025 17:24:35 +0800
Subject: [PATCH] 1、修改等待中间件返回结果超时时间为10秒; 2、修改向水肥机下发命令相关逻辑; 3、修改水肥机回复命令结果处理逻辑; 4、去除前端webSocket关闭产生异常日志记录。
---
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/largeScreen/WebSocketServer.java | 92 +++++++++++++++++++++++++++++++--------------
1 files changed, 63 insertions(+), 29 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
index 9bcf52c..53fe54c 100644
--- 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
@@ -1,6 +1,11 @@
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.*;
@@ -13,10 +18,13 @@
* @Author: liurunyu
* @Date: 2025/2/10 15:25
* @Description 瀹㈡埛绔紙娴忚鍣級姣忓缓绔嬩竴涓獁ebsocket杩炴帴锛屾湇鍔$灏变細鍒涘缓涓�涓猈ebSocketServer瀹炰緥
+ * 搴旂敤鍓嶆彁鏄瓨鍦ㄤ竴涓狢onfig绫伙紝濡傛湰妯″潡鐨刉ebSocketConfig锛岃繖鏃禓ServerEndpoint鍙桽pringBoot瀹瑰櫒
+ * 绠$悊浜嗭紙鍦ㄥ祵鍏ュ紡web Servlet鐜涓級
*/
@Slf4j
-@ServerEndpoint("/websocket/ls/{id}")
+@ServerEndpoint("/websocket/ls/{token}")
@Component
+@Scope("prototype") // 闈炲崟渚嬶紝姣忔璇锋眰閮戒細鍒涘缓鏂扮殑瀹炰緥
public class WebSocketServer {
// 闈欐�佸彉閲忥紝璁板綍褰撳墠鍦ㄧ嚎杩炴帴鏁�
@@ -28,26 +36,46 @@
// 瀹㈡埛绔繛鎺ヤ細璇濓紝閫氳繃瀹冪粰瀹㈡埛绔彂閫佹暟鎹�
private Session session;
- // 瀹㈡埛绔痠d
- private String id = "";
+ private String orgTag;
+
+ // 瀹㈡埛绔笂绾挎椂鍒�
+ //private String onLineDt ;
+ // 瀹㈡埛绔�
+ private String token = "";
+
+ @Autowired
+ private SsoCheck ssoCheck ;
/**
* 杩炴帴寤虹珛鎴愬姛璋冪敤鐨勬柟娉�
* @param session websocket浼氳瘽瀵硅薄
- * @param id 瀹㈡埛绔痠d
+ * @param token 瀹㈡埛绔痠d
*/
@OnOpen
- public void onOpen(Session session, @PathParam("id") String id) {
- this.session = session;
- this.id = id;
- this.sendMessage(WebSocketHeartBeat.getHeartBeatMessage());
+ 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(id)) {
- webSocketMap.remove(id);
- webSocketMap.put(id, this);
- } else {
- webSocketMap.put(id, this);
- WebSocketServer.addOnlineCount();
+ 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 ;
+ }
+ }
}
}
@@ -56,12 +84,12 @@
*/
@OnClose
public void onClose() {
- if (webSocketMap.containsKey(id)) {
- webSocketMap.remove(id);
+ if (webSocketMap.containsKey(token)) {
+ webSocketMap.remove(token);
//浠巗et涓垹闄�
WebSocketServer.subOnlineCount();
}
- log.info("瀹㈡埛绔�:" + id + "锛屽叧闂簡websocket");
+ log.info("瀹㈡埛绔�:" + token + "锛屽叧闂簡websocket");
}
/**
@@ -71,7 +99,7 @@
*/
@OnMessage
public void onMessage(String message, Session session) {
- log.info("瀹㈡埛绔�:" + id + "锛寃ebsocket鎶ユ枃:" + message);
+ log.info("瀹㈡埛绔�:" + token + "锛寃ebsocket鎶ユ枃:" + message);
}
/**
@@ -81,7 +109,7 @@
*/
@OnError
public void onError(Session session, Throwable error) {
- log.error("瀹㈡埛绔�:" + this.id + "锛寃ebsocket浼氳瘽寮傚父锛屽師鍥�:" + error.getMessage());
+ //log.error("瀹㈡埛绔�:" + this.token + "锛寃ebsocket浼氳瘽寮傚父锛屽師鍥�:" + error.getMessage());
}
/**
@@ -91,29 +119,35 @@
try{
this.session.getBasicRemote().sendText(message);
}catch (Exception e){
- log.error("瀹㈡埛绔�:" + id + "锛寃ebsocket缃戠粶鍙戦�佹暟鎹紓甯�", e);
+ log.error("瀹㈡埛绔�:" + token + "锛寃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 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 sendOneMessage(String message, String id) throws IOException {
- if (message != null && message.length() != 0 && webSocketMap.containsKey(id)) {
- webSocketMap.get(id).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("瀹㈡埛绔�" + id + "锛屼笉鍦ㄧ嚎锛�");
+ log.error("瀹㈡埛绔�" + token + "锛屼笉鍦ㄧ嚎锛�");
}
}
--
Gitblit v1.8.0