fix: ASW-148 novnc 实现超时关闭功能

This commit is contained in:
zhangshuai
2024-11-08 12:24:09 +08:00
parent de19724c1f
commit 37299e7180

View File

@@ -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<EnvironmentSessionEntity>()
.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<EnvironmentSessionEntity>()
.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); // 每分钟检查一次
});
}