whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/webSocket/PwrAlarmSocket.java
@@ -1,6 +1,8 @@
package com.whyc.webSocket;
import com.whyc.dto.Response;
import com.whyc.pojo.PwrdevAlarm;
import com.whyc.service.PwrDevAlarmVerifyService;
import com.whyc.service.PwrdevAlarmService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,51 +21,73 @@
    private static final int executeTime = 5000;
    private static boolean exit=false;
    private volatile boolean runFlag=true;
    private static Map<String,Thread> threadMap = new HashMap<>();
    private volatile Map<String,Thread> threadMap = new HashMap<>();
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    private static PwrdevAlarmService service;
    private Session session;
    @Autowired
    public void setService(PwrdevAlarmService service){
        PwrAlarmSocket.service = service;
    }
    @OnOpen
    public void onOpen(Session session){
        this.session=session;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        //停止当前socket的线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            threadBefore.interrupt();
            exit = true;
        }
    public synchronized void onMessage(Session session, String message){
        PwrdevAlarm pwrdevAlarm = ActionUtil.getGson().fromJson(message, PwrdevAlarm.class);
        thread = new Thread("Thread_pwrAlarm") {
            public void run() {
                exit=false;
                while (!thread.isInterrupted()&&!exit) {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        if (session.isOpen()) {
                            session.getBasicRemote().sendObject(service.getAllPage2(pwrdevAlarm));
                            //推送信息
                            synchronized (session) {
                                //session.getBasicRemote().sendObject(service.getAllPage2OfWebSocket(pwrdevAlarm));
                                //获取告警参数改为一个机房一个
                                session.getBasicRemote().sendObject(service.getAllPage2OfWebSocket_pid(pwrdevAlarm));
                            }
                            threadFlagMap.put(thread.getId(),false);
                        }
                        sleep(executeTime);
                    } catch (IOException | InterruptedException | EncodeException e) {
                        //} catch (IOException | InterruptedException | EncodeException e) {
                    } catch (Exception e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
        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(){
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
    @OnError
@@ -72,5 +96,6 @@
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}