whyclxw
2024-09-04 429c62013d12dd71d0542ced3a69c47d68c5466a
src/main/java/com/whyc/webSocket/DevInfSocket.java
@@ -1,9 +1,8 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Page;
import com.whyc.dto.DevInfDto;
import com.whyc.dto.Response;
import com.whyc.pojo.db_user.UserInf;
import com.whyc.service.DevInfService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,7 +11,8 @@
import javax.servlet.http.HttpSession;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
@@ -29,6 +29,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;
@@ -41,44 +47,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() {
        DevInfDto devInfDto = ActionUtil.getGson("yyyy-MM-dd").fromJson(message, DevInfDto.class);
        //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()) {
                        Response response=dinfService.getAllInf(userId,page);
                        session.getBasicRemote().sendObject(response);
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Response res=dinfService.getAllInf(userId, devInfDto);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(res);
                            }
                            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());
    }
}