1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| @Configuration @EnableWebSocket @Slf4j public class WebSocketConfig implements WebSocketConfigurer {
private Map<String, WebSocketSession> aiClientSessionsMap = new ConcurrentHashMap<>();
private Map<String, WebSocketSession> myClientSessionsMap = new ConcurrentHashMap<>();
public WebSocketSession getAiClientSession(String clientId) { try { return aiClientSessionsMap.get(clientId); } catch (Exception e) { log.error("Error occurred while getting WebSocket session for client: {}", clientId, e); return null; } }
public synchronized void addAiClientSession(String clientId, WebSocketSession session) { aiClientSessionsMap.put(clientId, session); log.info("Added WebSocket session for client: {}", clientId); }
public synchronized void removeAiClientSession(String clientId) { WebSocketSession session = aiClientSessionsMap.remove(clientId); if (session != null && session.isOpen()) { try { session.close(); } catch (IOException e) { log.error("Failed to close WebSocket session for client: {}", clientId, e); } } }
public WebSocketSession getMyClientSession(String clientId) { try { return myClientSessionsMap.get(clientId); } catch (Exception e) { log.error("Error occurred while getting WebSocket session for client: {}", clientId, e); return null; } }
public synchronized void addMyClientSession(String clientId, WebSocketSession session) { myClientSessionsMap.put(clientId, session); log.info("Added WebSocket session for client: {}", clientId); }
public synchronized void removeMyClientSession(String clientId) { WebSocketSession session = myClientSessionsMap.remove(clientId); if (session != null && session.isOpen()) { try { session.close(); } catch (IOException e) { log.error("Failed to close WebSocket session for client: {}", clientId, e); } } }
@Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new MyWebSocketHandler(this), "/websocket-endpoint") .setAllowedOrigins("*"); }
|