From 37299e718021eabd9c8a9c3b375c999ace9e0707 Mon Sep 17 00:00:00 2001 From: zhangshuai Date: Fri, 8 Nov 2024 12:24:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20ASW-148=20novnc=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=85=B3=E9=97=AD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnvironmentNovncWebSocketHandler.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java b/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java index d73b0c7..4d75453 100644 --- a/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java +++ b/src/main/java/net/geedge/asw/common/config/websocket/EnvironmentNovncWebSocketHandler.java @@ -69,31 +69,34 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler { this.sessionId = (String) session.getAttributes().get("sessionId"); this.userId = (String) session.getAttributes().get("userId"); Constants.ENV_NOVNC_WEBSOCKET_SESSION.put(sessionId, session); - lastReceivedTime = T.DateUtil.currentSeconds(); // 初始化接收时间 + lastReceivedTime = System.currentTimeMillis(); // 初始化接收时间 startConnectionMonitor(session); } private void startConnectionMonitor(WebSocketSession session) { - scheduler.scheduleAtFixedRate(() -> { - if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) { - try { - if (session.isOpen()) { - log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId); + Thread.ofVirtual().start(() -> { + while (true) { + if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) { + try { + if (session.isOpen()) { + log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId); - // update session status - environmentSessionService.update(new LambdaUpdateWrapper() - .set(EnvironmentSessionEntity::getStatus, 2) - .eq(EnvironmentSessionEntity::getId, sessionId)); - - session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed.")); + // update session status + environmentSessionService.update(new LambdaUpdateWrapper() + .set(EnvironmentSessionEntity::getStatus, 2) + .eq(EnvironmentSessionEntity::getId, sessionId)); + session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed.")); + break; + } + // 每5分钟检查一次 + Thread.sleep(300000); + } catch (Exception e) { + log.error(e, "Error clean no real use connection"); } - } catch (IOException e) { - log.error(e, "Error clean no real use connection"); } - scheduler.shutdown(); // 关闭调度器 } - }, 0, 1, TimeUnit.MINUTES); // 每分钟检查一次 + }); }