From ea5be111f899db0af7356bbee88c07161875cc54 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期一, 16 六月 2025 19:12:46 +0800
Subject: [PATCH] 领导层首页-实时告警信息

---
 src/main/java/com/whyc/webSocket/LeaderHomeSubModule3Socket.java |   82 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/whyc/webSocket/LeaderHomeSubModule3Socket.java b/src/main/java/com/whyc/webSocket/LeaderHomeSubModule3Socket.java
new file mode 100644
index 0000000..8a49048
--- /dev/null
+++ b/src/main/java/com/whyc/webSocket/LeaderHomeSubModule3Socket.java
@@ -0,0 +1,82 @@
+package com.whyc.webSocket;
+
+import com.whyc.config.WebSocketConfig;
+import com.whyc.dto.Response;
+import com.whyc.pojo.db_user.User;
+import com.whyc.service.LeaderHomeService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpSession;
+import javax.websocket.*;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+
+/**
+ * 棰嗗灞傞椤� 瀛愭ā鍧�
+ * 涓嶅悓鏃堕棿鍚屼竴鍝佺墝
+ */
+@Component
+@ServerEndpoint(value = "/leaderHome/BattDiffTimeSameBrand",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
+public class LeaderHomeSubModule3Socket {
+
+    private Session session;
+
+    private Thread thread;
+
+    private static HttpSession httpSession;
+    private static LeaderHomeService leaderHomeService;
+
+    @Autowired
+    public void setLeaderHomeService(LeaderHomeService leaderHomeService) {
+        LeaderHomeSubModule3Socket.leaderHomeService = leaderHomeService;
+    }
+
+    @OnOpen
+    public void onOpen(Session session, EndpointConfig config){
+        this.session = session;
+        httpSession = (HttpSession) config.getUserProperties().get("httpSession");
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message) {
+        //涓哄搧鐗�
+        String brand = message;
+        User user = (User) httpSession.getAttribute("user");
+        Integer userId = user.getId();
+        Thread thread = new Thread() {
+            @Override
+            public void run() {
+                try {
+                    while (!currentThread().isInterrupted()) {
+                        Response response = leaderHomeService.getBattPerformanceOfDiffTimeSameBrand(userId, brand);
+                        session.getBasicRemote().sendObject(new Response().set(1, response));
+                        sleep(4000);
+                    }
+                } catch (Exception e) {
+                    this.interrupt();
+                }
+            }
+        };
+        thread.start();
+        this.thread = thread;
+    }
+
+    @OnClose
+    public void onClose(CloseReason closeReason) throws IOException {
+        System.err.println("closeReason = " + closeReason);
+        if(session.isOpen()){
+            session.close();
+        }
+    }
+
+    @OnError
+    public void onError(Throwable error) throws IOException {
+        error.printStackTrace();
+        thread.isInterrupted();
+        if(session.isOpen()){
+            session.close();
+        }
+    }
+
+}

--
Gitblit v1.9.1