diff --git a/src/utils/serverRotation.js b/src/utils/serverRotation.js index 834b4aa..90a181e 100644 --- a/src/utils/serverRotation.js +++ b/src/utils/serverRotation.js @@ -36,7 +36,8 @@ export function getServerList(provider) { */ export async function tryWithRotation(operation, options = {}) { const provider = options.provider || getSetting("server.provider"); - const onServerTried = options.onServerTried || (() => {}); + const onServerTried = options.onServerTried; + const hasCallback = typeof onServerTried === 'function'; const servers = getServerList(provider); const triedServers = []; @@ -45,19 +46,25 @@ export async function tryWithRotation(operation, options = {}) { for (const serverUrl of servers) { try { triedServers.push({ url: serverUrl, status: "trying" }); - onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] }); + if (hasCallback) { + onServerTried({ url: serverUrl, status: "trying", tried: [...triedServers] }); + } const result = await operation(serverUrl); triedServers[triedServers.length - 1].status = "success"; - onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] }); + if (hasCallback) { + onServerTried({ url: serverUrl, status: "success", tried: [...triedServers] }); + } return result; } catch (error) { lastError = error; triedServers[triedServers.length - 1].status = "failed"; triedServers[triedServers.length - 1].error = error.message || String(error); - onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] }); + if (hasCallback) { + onServerTried({ url: serverUrl, status: "failed", error, tried: [...triedServers] }); + } // Continue to next server console.warn(`Server ${serverUrl} failed:`, error.message); diff --git a/src/utils/socketClient.js b/src/utils/socketClient.js index f0c0162..5f86814 100644 --- a/src/utils/socketClient.js +++ b/src/utils/socketClient.js @@ -35,9 +35,10 @@ export function getSocket() { } connectedDomain = serverUrl; - // For classworkscloud, create socket with rotation support - // The socket will initially connect to the first server - // If connection fails, Socket.IO will handle reconnection + // For classworkscloud, create socket with the first server in rotation + // Note: Socket.IO's built-in reconnection will retry the same server URL. + // Server rotation is handled at the HTTP request level, not Socket.IO level. + // If the Socket.IO server goes down, the connection will fail until the server recovers. socket = io(serverUrl, {transports: ["polling","websocket"]}); // Re-attach previously registered event handlers on new socket instance