whyclxw
2025-01-13 000e53dd00093a0886aaa4ae29d03bd20edd3592
普通用户登录查看自己授权记录--改socket
1个文件已添加
3个文件已修改
116 ■■■■■ 已修改文件
src/main/java/com/whyc/mapper/LockInfMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/AuthiruzeInfService.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/AuthUnameSocket.java 100 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/LockInfMapper.xml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/LockInfMapper.java
@@ -14,4 +14,7 @@
    //查看区域下所有的锁
    List<LockInf> selectAllLockById(@Param("areaList") List<Integer> areaList);
    //根据锁的id查询锁的记录
    LockInf selectlinfByLockId(Integer lockId);
}
src/main/java/com/whyc/service/AuthiruzeInfService.java
@@ -208,10 +208,8 @@
        List<AuthiruzeInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AuthiruzeInf auth:list) {
                QueryWrapper wrapper1=new QueryWrapper();
                wrapper1.eq("lock_id",auth.getLockId());
                wrapper1.last("limit 1");
                LockInf linf=lockInfMapper.selectOne(wrapper1);
                //根据锁的id查询锁的记录
                LockInf linf=lockInfMapper.selectlinfByLockId(auth.getLockId());
                auth.setLinf(linf);
            }
        }
src/main/java/com/whyc/webSocket/AuthUnameSocket.java
New file
@@ -0,0 +1,100 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.service.AreaInfService;
import com.whyc.service.AuthiruzeInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
/**
 * 普通用户登录查看自己授权记录-----app
 */
@Component
@ServerEndpoint(value = "/authUname",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class AuthUnameSocket {
    private Session session;
    private Thread thread;
    private static AuthiruzeInfService authService;
    private volatile boolean runFlag = true;
    private volatile Map<String, Thread> threadMap = new HashMap<>();
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    @Autowired
    public void setAuthiruzeInfService(AuthiruzeInfService authService) {
        AuthUnameSocket.authService = authService;
    }
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
    }
    @OnMessage
    public void onMessage(Session session, String message) {
        String uname= String.valueOf(message);
        thread = new Thread("Thread_authUnameSocket") {
            @Override
            public void run() {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Response res=authService.getAuthByUid(uname);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(res);
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(4000);
                    } 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(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}
src/main/resources/mapper/LockInfMapper.xml
@@ -40,4 +40,11 @@
        </where>
        order by id asc
    </select>
    <select id="selectlinfByLockId" resultType="com.whyc.pojo.db_area.LockInf">
        select tb_lock_inf.*,tb_area_inf.area_name,tb_area_inf.area_path from db_area.tb_lock_inf,db_area.tb_area_inf
        <where>
            tb_lock_inf.area_id=tb_area_inf.id
            and lock_id=#{lockId}
        </where>
    </select>
</mapper>