From 86480602129429a2dbf5c3743f93049feaa7bb72 Mon Sep 17 00:00:00 2001
From: whyclxw <810412026@qq.com>
Date: 星期三, 28 八月 2024 16:19:01 +0800
Subject: [PATCH] 设备实时状态推送

---
 src/main/java/com/whyc/webSocket/DevInfSocket.java |   66 ++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/whyc/webSocket/DevInfSocket.java b/src/main/java/com/whyc/webSocket/DevInfSocket.java
index 9982060..1f9abbd 100644
--- a/src/main/java/com/whyc/webSocket/DevInfSocket.java
+++ b/src/main/java/com/whyc/webSocket/DevInfSocket.java
@@ -13,6 +13,11 @@
 import javax.websocket.*;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 
 /**
@@ -29,6 +34,12 @@
 
     private HttpSession httpSession;
 
+    private volatile boolean runFlag = true;
+
+    private volatile Map<String, Thread> threadMap = new HashMap<>();
+
+    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
+
     @Autowired
     public void setDevInfService(DevInfService dinfService) {
         DevInfSocket.dinfService = dinfService;
@@ -42,43 +53,62 @@
     @OnMessage
     public void onMessage(Session session, String message) {
         Page page= ActionUtil.getGson().fromJson(message,Page.class);
-        UserInf user = (UserInf) this.httpSession.getAttribute("user");
-        final int userId = user.getUid();
-        //final int userId = 1087;
-        Thread thread = new Thread() {
+        //UserInf user = (UserInf) this.httpSession.getAttribute("user");
+        //final int userId = user.getUid();
+        final int userId = 101;
+        thread = new Thread("Thread_DinfSocket") {
             @Override
             public void run() {
-                try {
-                    while (!currentThread().isInterrupted()) {
+                Map<String, Object> res = new HashMap<>();
+                while (runFlag && !isInterrupted()) {
+                    Thread thread = currentThread();
+                    threadFlagMap.put(thread.getId(), true);
+                    try {
                         Response response=dinfService.getAllInf(userId,page);
-                        session.getBasicRemote().sendObject(response);
+                        if (session.isOpen()) {
+                            //鎺ㄩ�佷俊鎭�
+                            synchronized (session) {
+                                session.getBasicRemote().sendObject(new Response().set(1, response));
+                            }
+                            threadFlagMap.put(thread.getId(), false);
+                        }
                         sleep(4000);
+                    } catch (Exception e) {
+                        interrupt();
                     }
-                } catch (Exception e) {
-                    this.interrupt();
                 }
             }
         };
         thread.start();
-        this.thread = thread;
+        threadFlagMap.put(thread.getId(),true);
+        //鍋滄鑰佺殑socket绾跨▼
+        Thread threadBefore = threadMap.get(session.getId());
+        if(threadBefore !=null && threadBefore.isAlive()){
+            while (threadFlagMap.get(threadBefore.getId())){
+            }
+            threadBefore.interrupt();
+        }
+        //灏嗙嚎绋嬪瓨鍌�,渚夸簬璋冪敤瀹氫綅
+        threadMap.put(session.getId(), this.thread);
     }
 
 
     @OnClose
-    public void onClose(CloseReason closeReason) throws IOException {
+    public void onClose(CloseReason closeReason){
         System.err.println("closeReason = " + closeReason);
-        if(session.isOpen()){
-            session.close();
+        runFlag = false;
+        if (thread != null && thread.isAlive()) {
+            thread.interrupt();
         }
+        threadMap.remove(session.getId());
     }
 
     @OnError
-    public void onError(Throwable error) throws IOException {
+    public void onError(Throwable error) {
         error.printStackTrace();
-        thread.isInterrupted();
-        if(session.isOpen()){
-            session.close();
+        if (thread != null && thread.isAlive()) {
+            thread.interrupt();
         }
+        threadMap.remove(session.getId());
     }
-
 }
\ No newline at end of file

--
Gitblit v1.9.1