| | |
| | | package com.ltkj.web.websocket; |
| | | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.ltkj.common.core.domain.model.LoginUser; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.socket.CloseStatus; |
| | |
| | | import org.springframework.web.socket.handler.TextWebSocketHandler; |
| | | import redis.clients.jedis.util.SafeEncoder; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Company: 西安路泰科技有限公司 |
| | |
| | | @Component |
| | | public class WebSockerManager extends TextWebSocketHandler { |
| | | |
| | | private static final ConcurrentHashMap<Long, ClientSessionInfo> userSessions = new ConcurrentHashMap<>(); |
| | | public final ConcurrentHashMap<String, ClientSessionInfo> userSessions = new ConcurrentHashMap<>(); |
| | | private static final ConcurrentHashMap<String, WebSocketClientInfo> sessions = new ConcurrentHashMap<>(); |
| | | |
| | | private WebSocketClientInfo getUser(WebSocketSession session) { |
| | | // long uid = Long.parseLong(session.getAttributes().get("uid").toString()); |
| | | // String hospId = session.getAttributes().get("hospId").toString(); |
| | | // WebSocketClientInfo info = new WebSocketClientInfo(); |
| | | // info.setUid(uid); |
| | | // info.setHospId(hospId); |
| | | // return info; |
| | | WebSocketClientInfo clientInfo = new WebSocketClientInfo(); |
| | | clientInfo.setUid(123L); |
| | | clientInfo.setHospId("hosp"); |
| | | return clientInfo; |
| | | LoginUser loginUser = (LoginUser) session.getAttributes().get("user"); |
| | | WebSocketClientInfo info = new WebSocketClientInfo(); |
| | | info.setUid(Long.parseLong(loginUser.getUserId())); |
| | | info.setHospId(loginUser.getHospName()); |
| | | return info; |
| | | // WebSocketClientInfo clientInfo = new WebSocketClientInfo(); |
| | | // clientInfo.setUid(123L); |
| | | // clientInfo.setHospId("hosp"); |
| | | // return clientInfo; |
| | | } |
| | | |
| | | @Override |
| | |
| | | WebSocketClientInfo clientInfo = getUser(session); |
| | | // userSessions.put(clientInfo.getUid(), new ClientSessionInfo(session, clientInfo.getHospId())); |
| | | sessions.put(session.getId(),clientInfo); |
| | | log.info(JSONUtil.toJsonStr(sessions)); |
| | | ClientSessionInfo sessionInfo = new ClientSessionInfo(); |
| | | sessionInfo.setSession(session); |
| | | sessionInfo.setCode(clientInfo.getHospId()); |
| | | userSessions.put(String.valueOf(clientInfo.getUid()), sessionInfo); |
| | | session.sendMessage(new TextMessage("连接成功")); |
| | | log.info("[WebSocket] 在线列表UserIds\n{}",JSONUtil.toJsonStr(new ArrayList<>(userSessions.keySet()))); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
| | | log.info("[WebSocket] 连接关闭: " + status); |
| | | WebSocketClientInfo clientInfo = sessions.get(session.getId()); |
| | | userSessions.remove(String.valueOf(clientInfo.getUid())); |
| | | } |
| | | } |