whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/webSocket/InterfaceBattAlarmSocket.java
@@ -1,45 +1,54 @@
package com.whyc.webSocket;
import com.whyc.dto.InterfacePageParam;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.InterfaceParam;
import com.whyc.service.InterfaceService;
import com.whyc.util.ActionUtil;
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 = "/interfaceBattAlarm",encoders = WebSocketEncoder.class)
@ServerEndpoint(value = "/interfaceBattAlarm", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
public class InterfaceBattAlarmSocket {
    private volatile Thread thread;
    private static final int executeTime = 10000;
    private volatile boolean runFlag=true;
    private volatile boolean runFlag = true;
    private volatile Map<String,Thread> threadMap = new HashMap<>();
    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 InterfaceService service;
    private Session session;
    private HttpSession httpSession;
    @Autowired
    public void setService(InterfaceService service){
    public void setService(InterfaceService service) {
        InterfaceBattAlarmSocket.service = 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 message){
        InterfacePageParam alarmPar = ActionUtil.getGson().fromJson(message, InterfacePageParam.class);
        InterfaceParam param = ActionUtil.getGson().fromJson(message, InterfaceParam.class);
        String lang = (String) this.httpSession.getAttribute("lang");
        thread = new Thread("Thread_interfaceBattAlarm") {
            @Override
            public void run() {
                while (runFlag&&!isInterrupted()) {
                    Thread thread = currentThread();
@@ -48,7 +57,7 @@
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(service.getBattAlarm(alarmPar.getPageNum(),alarmPar.getPageSize()));
                                session.getBasicRemote().sendObject(service.getBattAlarm(param.getBattgroupIds(), param.getInterfaceNumber(), lang));
                            }
                            threadFlagMap.put(thread.getId(),false);
                        }