diff --git a/VERSION.properties b/VERSION.properties index 68a1b9e3..30c303f4 100644 --- a/VERSION.properties +++ b/VERSION.properties @@ -1,2 +1,2 @@ -client.version=1.20.42 +client.version=1.20.43 server.version=1.16.24 diff --git a/shine-UI/js/app.js b/shine-UI/js/app.js index 74b6b4de..bb86251b 100644 --- a/shine-UI/js/app.js +++ b/shine-UI/js/app.js @@ -2133,4 +2133,17 @@ async function init() { }); } +const SESSION_HANDSHAKE_OPS = new Set([ + 'AuthChallenge', + 'CreateAuthSession', + 'SessionChallenge', + 'SessionLogin', +]); + +authService.ws.setRequestGuard(async (op) => { + if (SESSION_HANDSHAKE_OPS.has(op)) return; + if (!state.session.isAuthorized || authService.ws.isSessionReady()) return; + await ensureSessionAfterWsReconnect(); +}); + init(); diff --git a/shine-UI/js/services/auth-service.js b/shine-UI/js/services/auth-service.js index 19b9f1ef..aac20b62 100644 --- a/shine-UI/js/services/auth-service.js +++ b/shine-UI/js/services/auth-service.js @@ -1004,9 +1004,11 @@ export class AuthService { resetConnection(serverUrl = this.serverUrl, { clearSessionContext = true } = {}) { const normalized = normalizeServerUrl(serverUrl); + const requestGuard = this.ws?.requestGuard; try { this.ws?.close(); } catch {} this.serverUrl = normalized; this.ws = new WsJsonClient(this.serverUrl); + this.ws.setRequestGuard(requestGuard); this.headerHashCache = new Map(); this.lineTailCache = new Map(); this.lineWriteQueues = new Map(); @@ -1185,6 +1187,7 @@ export class AuthService { const sessionId = createResp?.payload?.sessionId; if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); + this.ws.markSessionReady(); const connectionScope = String(createResp?.payload?.connectionScope || '').trim().toUpperCase(); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); @@ -1275,6 +1278,7 @@ export class AuthService { const sessionId = createResp?.payload?.sessionId; if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); + tempAuth.ws.markSessionReady(); const canonicalLogin = await tempAuth.resolveCanonicalDisplayLogin(cleanLogin); @@ -1351,6 +1355,7 @@ export class AuthService { const storagePwd = loginResp?.payload?.storagePwd; if (!storagePwd) throw new Error('SessionLogin: не вернулся storagePwd'); + this.ws.markSessionReady(); const connectionScope = String(loginResp?.payload?.connectionScope || '').trim().toUpperCase(); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); diff --git a/shine-UI/js/services/ws-client.js b/shine-UI/js/services/ws-client.js index b0c4878c..bb22b553 100644 --- a/shine-UI/js/services/ws-client.js +++ b/shine-UI/js/services/ws-client.js @@ -64,6 +64,22 @@ export class WsJsonClient { this.pending = new Map(); this.openPromise = null; this.eventListeners = new Map(); + this.requestGuard = null; + this.connectionGeneration = 0; + this.sessionReadyGeneration = 0; + } + + setRequestGuard(guard) { + this.requestGuard = typeof guard === 'function' ? guard : null; + } + + markSessionReady() { + this.sessionReadyGeneration = this.connectionGeneration; + } + + isSessionReady() { + return this.connectionGeneration > 0 + && this.sessionReadyGeneration === this.connectionGeneration; } async open() { @@ -82,6 +98,7 @@ export class WsJsonClient { this.openPromise = new Promise((resolve, reject) => { const ws = new WebSocket(this.url); this.ws = ws; + this.connectionGeneration += 1; ws.addEventListener('open', () => { resolve(); @@ -97,7 +114,10 @@ export class WsJsonClient { }, { once: true }); ws.addEventListener('close', () => { - this.failPending(t('remaining.websocket.websocket_connection_closed')); + if (this.ws === ws) { + this.ws = null; + this.failPending(t('remaining.websocket.websocket_connection_closed')); + } }); ws.addEventListener('message', (event) => { @@ -111,7 +131,14 @@ export class WsJsonClient { } async request(op, payload = {}, timeoutMs = DEFAULT_TIMEOUT_MS) { - await this.open(); + for (let attempt = 0; attempt < 2; attempt += 1) { + await this.open(); + if (this.requestGuard) await this.requestGuard(op); + if (this.ws && this.ws.readyState === WebSocket.OPEN) break; + } + if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { + throw new Error(t('remaining.websocket.websocket_connection_closed')); + } const requestId = createRequestId(op); const body = { op, requestId, payload }; @@ -149,8 +176,10 @@ export class WsJsonClient { close() { if (this.ws) { - this.ws.close(); + const ws = this.ws; this.ws = null; + this.failPending(t('remaining.websocket.websocket_connection_closed')); + ws.close(); } }