Исправить восстановление сессии после переподключения WS

This commit is contained in:
AidarKC
2026-11-15 14:35:28 +03:00
parent 70540ff041
commit e01da7ae87
4 changed files with 51 additions and 4 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
client.version=1.20.42 client.version=1.20.43
server.version=1.16.24 server.version=1.16.24
+13
View File
@@ -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(); init();
+5
View File
@@ -1004,9 +1004,11 @@ export class AuthService {
resetConnection(serverUrl = this.serverUrl, { clearSessionContext = true } = {}) { resetConnection(serverUrl = this.serverUrl, { clearSessionContext = true } = {}) {
const normalized = normalizeServerUrl(serverUrl); const normalized = normalizeServerUrl(serverUrl);
const requestGuard = this.ws?.requestGuard;
try { this.ws?.close(); } catch {} try { this.ws?.close(); } catch {}
this.serverUrl = normalized; this.serverUrl = normalized;
this.ws = new WsJsonClient(this.serverUrl); this.ws = new WsJsonClient(this.serverUrl);
this.ws.setRequestGuard(requestGuard);
this.headerHashCache = new Map(); this.headerHashCache = new Map();
this.lineTailCache = new Map(); this.lineTailCache = new Map();
this.lineWriteQueues = new Map(); this.lineWriteQueues = new Map();
@@ -1185,6 +1187,7 @@ export class AuthService {
const sessionId = createResp?.payload?.sessionId; const sessionId = createResp?.payload?.sessionId;
if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId');
this.ws.markSessionReady();
const connectionScope = String(createResp?.payload?.connectionScope || '').trim().toUpperCase(); const connectionScope = String(createResp?.payload?.connectionScope || '').trim().toUpperCase();
const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin);
@@ -1275,6 +1278,7 @@ export class AuthService {
const sessionId = createResp?.payload?.sessionId; const sessionId = createResp?.payload?.sessionId;
if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId'); if (!sessionId) throw new Error('CreateAuthSession: не вернулся sessionId');
tempAuth.ws.markSessionReady();
const canonicalLogin = await tempAuth.resolveCanonicalDisplayLogin(cleanLogin); const canonicalLogin = await tempAuth.resolveCanonicalDisplayLogin(cleanLogin);
@@ -1351,6 +1355,7 @@ export class AuthService {
const storagePwd = loginResp?.payload?.storagePwd; const storagePwd = loginResp?.payload?.storagePwd;
if (!storagePwd) throw new Error('SessionLogin: не вернулся storagePwd'); if (!storagePwd) throw new Error('SessionLogin: не вернулся storagePwd');
this.ws.markSessionReady();
const connectionScope = String(loginResp?.payload?.connectionScope || '').trim().toUpperCase(); const connectionScope = String(loginResp?.payload?.connectionScope || '').trim().toUpperCase();
const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin); const canonicalLogin = await this.resolveCanonicalDisplayLogin(cleanLogin);
+30 -1
View File
@@ -64,6 +64,22 @@ export class WsJsonClient {
this.pending = new Map(); this.pending = new Map();
this.openPromise = null; this.openPromise = null;
this.eventListeners = new Map(); 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() { async open() {
@@ -82,6 +98,7 @@ export class WsJsonClient {
this.openPromise = new Promise((resolve, reject) => { this.openPromise = new Promise((resolve, reject) => {
const ws = new WebSocket(this.url); const ws = new WebSocket(this.url);
this.ws = ws; this.ws = ws;
this.connectionGeneration += 1;
ws.addEventListener('open', () => { ws.addEventListener('open', () => {
resolve(); resolve();
@@ -97,7 +114,10 @@ export class WsJsonClient {
}, { once: true }); }, { once: true });
ws.addEventListener('close', () => { ws.addEventListener('close', () => {
if (this.ws === ws) {
this.ws = null;
this.failPending(t('remaining.websocket.websocket_connection_closed')); this.failPending(t('remaining.websocket.websocket_connection_closed'));
}
}); });
ws.addEventListener('message', (event) => { ws.addEventListener('message', (event) => {
@@ -111,7 +131,14 @@ export class WsJsonClient {
} }
async request(op, payload = {}, timeoutMs = DEFAULT_TIMEOUT_MS) { async request(op, payload = {}, timeoutMs = DEFAULT_TIMEOUT_MS) {
for (let attempt = 0; attempt < 2; attempt += 1) {
await this.open(); 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 requestId = createRequestId(op);
const body = { op, requestId, payload }; const body = { op, requestId, payload };
@@ -149,8 +176,10 @@ export class WsJsonClient {
close() { close() {
if (this.ws) { if (this.ws) {
this.ws.close(); const ws = this.ws;
this.ws = null; this.ws = null;
this.failPending(t('remaining.websocket.websocket_connection_closed'));
ws.close();
} }
} }