fix: ASW-148 断开连接后关闭检测线程
This commit is contained in:
@@ -64,14 +64,15 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
|
|||||||
this.userId = (String) session.getAttributes().get("userId");
|
this.userId = (String) session.getAttributes().get("userId");
|
||||||
Constants.ENV_NOVNC_WEBSOCKET_SESSION.put(sessionId, session);
|
Constants.ENV_NOVNC_WEBSOCKET_SESSION.put(sessionId, session);
|
||||||
lastReceivedTime = System.currentTimeMillis(); // 初始化接收时间
|
lastReceivedTime = System.currentTimeMillis(); // 初始化接收时间
|
||||||
startConnectionMonitor(session);
|
Thread checkSessionTimeout = startConnectionMonitor(session);
|
||||||
|
session.getAttributes().put("checkSessionTimeoutThread", checkSessionTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startConnectionMonitor(WebSocketSession session) {
|
private Thread startConnectionMonitor(WebSocketSession session) {
|
||||||
Thread.ofVirtual().start(() -> {
|
Thread thread = Thread.ofVirtual().start(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) {
|
|
||||||
try {
|
try {
|
||||||
|
if (System.currentTimeMillis() - lastReceivedTime > sessionTimeout) {
|
||||||
if (session.isOpen()) {
|
if (session.isOpen()) {
|
||||||
log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId);
|
log.info("current no connection info, clean no real use connection finshed, sessionId: {}", sessionId);
|
||||||
|
|
||||||
@@ -83,14 +84,17 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
|
|||||||
session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed."));
|
session.close(CloseStatus.NORMAL.withReason("current no connection info, clean no real use connection finshed."));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// 每5分钟检查一次
|
}
|
||||||
Thread.sleep(300000);
|
Thread.sleep(300000);
|
||||||
} catch (Exception e) {
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
} catch (IOException e) {
|
||||||
log.error(e, "Error clean no real use connection");
|
log.error(e, "Error clean no real use connection");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -188,6 +192,11 @@ public class EnvironmentNovncWebSocketHandler extends TextWebSocketHandler {
|
|||||||
if (envWebsocket != null) {
|
if (envWebsocket != null) {
|
||||||
envWebsocket.sendClose(WebSocket.NORMAL_CLOSURE, "Normal closure");
|
envWebsocket.sendClose(WebSocket.NORMAL_CLOSURE, "Normal closure");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Thread checkSessionTimeoutThread = (Thread) session.getAttributes().get("checkSessionTimeoutThread");
|
||||||
|
if (checkSessionTimeoutThread != null) {
|
||||||
|
checkSessionTimeoutThread.interrupt();
|
||||||
|
}
|
||||||
Constants.ENV_NOVNC_WEBSOCKET_SESSION.remove(sessionId);
|
Constants.ENV_NOVNC_WEBSOCKET_SESSION.remove(sessionId);
|
||||||
super.afterConnectionClosed(session, status);
|
super.afterConnectionClosed(session, status);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user