From 9794b62ec85a8ca03a718311b8916dd9a59e0d5b Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 20 Jul 2021 15:47:59 +0300 Subject: [PATCH] GramJs: Temporary workaround for logouts on launch, add WS logging (#1290) --- src/api/gramjs/methods/client.ts | 13 ++++++++----- src/lib/gramjs/client/TelegramClient.js | 1 + src/lib/gramjs/client/auth.ts | 1 + src/lib/gramjs/extensions/PromisedWebSockets.js | 4 +++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/api/gramjs/methods/client.ts b/src/api/gramjs/methods/client.ts index 764b6eff..d6b40266 100644 --- a/src/api/gramjs/methods/client.ts +++ b/src/api/gramjs/methods/client.ts @@ -79,12 +79,15 @@ export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) initialMethod: platform === 'iOS' || platform === 'Android' ? 'phoneNumber' : 'qrCode', }); } catch (err) { - onUpdate({ - '@type': 'updateConnectionState', - connectionState: 'connectionStateBroken', - }); + // TODO Investigate which request causes this exception + if (err.message !== 'Disconnect') { + onUpdate({ + '@type': 'updateConnectionState', + connectionState: 'connectionStateBroken', + }); - return; + return; + } } if (DEBUG) { diff --git a/src/lib/gramjs/client/TelegramClient.js b/src/lib/gramjs/client/TelegramClient.js index 56ea934c..2add4354 100644 --- a/src/lib/gramjs/client/TelegramClient.js +++ b/src/lib/gramjs/client/TelegramClient.js @@ -614,6 +614,7 @@ class TelegramClient { * @param request * @returns {Promise} */ + async invoke(request) { if (request.classType !== 'request') { throw new Error('You can only invoke MTProtoRequests'); diff --git a/src/lib/gramjs/client/auth.ts b/src/lib/gramjs/client/auth.ts index f3bdbd6e..ac91e0a9 100644 --- a/src/lib/gramjs/client/auth.ts +++ b/src/lib/gramjs/client/auth.ts @@ -56,6 +56,7 @@ export async function checkAuthorization(client: TelegramClient) { await client.invoke(new Api.updates.GetState()); return true; } catch (e) { + if (e.message === 'Disconnect') throw e; return false; } } diff --git a/src/lib/gramjs/extensions/PromisedWebSockets.js b/src/lib/gramjs/extensions/PromisedWebSockets.js index cce61e68..afaa9b65 100644 --- a/src/lib/gramjs/extensions/PromisedWebSockets.js +++ b/src/lib/gramjs/extensions/PromisedWebSockets.js @@ -88,7 +88,9 @@ class PromisedWebSockets { this.client.onerror = (error) => { reject(error); }; - this.client.onclose = () => { + this.client.onclose = (event) => { + // eslint-disable-next-line no-console + console.error(`Socket closed with code: ${event.code}`); this.resolveRead(false); this.closed = true; };