adjust: code structure
This commit is contained in:
@@ -368,8 +368,31 @@
|
||||
<script>
|
||||
let ws;
|
||||
let shouldAutoScroll = true;
|
||||
let isConnecting = false;
|
||||
let reconnectTimer = null;
|
||||
|
||||
function connectWebSocket() {
|
||||
if (isConnecting || (ws && ws.readyState === WebSocket.CONNECTING)) {
|
||||
console.log("WebSocket connection already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
console.log("WebSocket already connected");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ws) {
|
||||
ws.close();
|
||||
ws = null;
|
||||
}
|
||||
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
|
||||
isConnecting = true;
|
||||
const token = document.getElementById("token-input").value;
|
||||
const wsProtocol =
|
||||
window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||
@@ -378,6 +401,7 @@
|
||||
|
||||
ws.onopen = function () {
|
||||
console.log("Connected to WebSocket");
|
||||
isConnecting = false;
|
||||
setStoredToken(token);
|
||||
document.getElementById("token-screen").style.display = "none";
|
||||
document.querySelector(".container-fluid").style.display = "flex";
|
||||
@@ -404,15 +428,21 @@
|
||||
};
|
||||
|
||||
ws.onerror = function () {
|
||||
isConnecting = false;
|
||||
document.getElementById("token-error").style.display = "block";
|
||||
};
|
||||
|
||||
ws.onclose = function () {
|
||||
isConnecting = false;
|
||||
console.log("Disconnected from WebSocket");
|
||||
document.getElementById("status").textContent =
|
||||
"WebSocket Status: Disconnected";
|
||||
document.getElementById("status").classList.remove("connected");
|
||||
setTimeout(connectWebSocket, 3000);
|
||||
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
}
|
||||
reconnectTimer = setTimeout(connectWebSocket, 3000);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -480,6 +510,11 @@
|
||||
sendWs("Quit");
|
||||
if (ws) {
|
||||
ws.close();
|
||||
ws = null;
|
||||
}
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user