From bb6f09aeedfd58b871ad0afa08392056da39e77f Mon Sep 17 00:00:00 2001
From: lxw <810412026@qq.com>
Date: 星期一, 24 四月 2023 19:06:10 +0800
Subject: [PATCH] 国际化告警滚动

---
 src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java |   47 +++++++++++++++++++++++++++++------------------
 1 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java b/src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java
index 6a17d8e..9bd3617 100644
--- a/src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java
+++ b/src/main/java/com/whyc/webSocket/SendAlarmDataSocket.java
@@ -1,5 +1,6 @@
 package com.whyc.webSocket;
 
+import com.whyc.config.WebSocketConfig;
 import com.whyc.dto.Response;
 import com.whyc.service.BattalarmDataService;
 import com.whyc.service.DevalarmDataService;
@@ -7,13 +8,14 @@
 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.util.HashMap;
 import java.util.Map;
 
 @Component
-@ServerEndpoint(value = "/sendAlarmData",encoders = WebSocketEncoder.class)
+@ServerEndpoint(value = "/sendAlarmData", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
 public class SendAlarmDataSocket {
     private volatile Thread thread;
 
@@ -23,7 +25,7 @@
 
     private volatile Map<String,Thread> threadMap = new HashMap<>();
 
-    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
+    private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
 
     private static BattalarmDataService battalarmDataService;
 
@@ -32,51 +34,60 @@
     private static PwrdevAlarmService pwrdevAlarmService;
 
     private Session session;
+
+    private HttpSession httpSession;
+
     @Autowired
-    public void setService(BattalarmDataService service){
+    public void setService(BattalarmDataService service) {
         SendAlarmDataSocket.battalarmDataService = service;
     }
+
     @Autowired
-    public void setService(DevalarmDataService service){
+    public void setService(DevalarmDataService service) {
         SendAlarmDataSocket.devalarmDataService = service;
     }
+
     @Autowired
-    public void setService(PwrdevAlarmService service){
+    public void setService(PwrdevAlarmService service) {
         SendAlarmDataSocket.pwrdevAlarmService = service;
     }
 
     @OnOpen
-    public void onOpen(Session session){
-        this.session=session;
+    public void onOpen(Session session, EndpointConfig config) {
+        this.session = session;
+        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
     }
+
     @OnMessage
-    public synchronized void onMessage(Session session, String uId){
+    public synchronized void onMessage(Session session, String uId) {
         int userId = Integer.parseInt(uId);
+        String lang = (String) this.httpSession.getAttribute("lang");
         thread = new Thread("Thread_sendAlarmData") {
+            @Override
             public void run() {
-                while (runFlag&&!isInterrupted()) {
+                while (runFlag && !isInterrupted()) {
                     Thread thread = currentThread();
-                    threadFlagMap.put(thread.getId(),true);
+                    threadFlagMap.put(thread.getId(), true);
                     try {
-                        Map<String, Response> res=new HashMap<>();
+                        Map<String, Response> res = new HashMap<>();
                         //鐢垫睜鍛婅
-                        Response battAlarm=battalarmDataService.getSendBattAlarmOfWebSocket(userId);
-                        res.put("battAlarm",battAlarm);
+                        Response battAlarm = battalarmDataService.getSendBattAlarmOfWebSocket(userId, lang);
+                        res.put("battAlarm", battAlarm);
                         //璁惧鍛婅
-                        Response devAlarm=devalarmDataService.getSendDevAlarmOfWebSocket(userId);
-                        res.put("devAlarm",devAlarm);
+                        Response devAlarm = devalarmDataService.getSendDevAlarmOfWebSocket(userId);
+                        res.put("devAlarm", devAlarm);
                         //鐢垫簮鍛婅
                         Response pwrAlarm = pwrdevAlarmService.getSendPwrAlarmOfWebSocket(userId);
-                        res.put("pwrAlarm",pwrAlarm);
+                        res.put("pwrAlarm", pwrAlarm);
                         if (session.isOpen()) {
                             //鎺ㄩ�佷俊鎭�
                             synchronized (session) {
                                 session.getBasicRemote().sendObject(new Response().set(1, res));
                             }
-                            threadFlagMap.put(thread.getId(),false);
+                            threadFlagMap.put(thread.getId(), false);
                         }
                         sleep(executeTime);
-                    //} catch (IOException | InterruptedException | EncodeException e) {
+                        //} catch (IOException | InterruptedException | EncodeException e) {
                     } catch (Exception e) {
                         interrupt();
                     }

--
Gitblit v1.9.1