diff --git a/src/lib/mtproto/mtproto.worker.ts b/src/lib/mtproto/mtproto.worker.ts index d658961d..b65dfa05 100644 --- a/src/lib/mtproto/mtproto.worker.ts +++ b/src/lib/mtproto/mtproto.worker.ts @@ -86,6 +86,10 @@ const taskListeners = { userAgent: (task: any) => { networkerFactory.userAgent = task.payload; + }, + + online: () => { + networkerFactory.forceReconnectTimeout(); } }; diff --git a/src/lib/mtproto/mtprotoworker.ts b/src/lib/mtproto/mtprotoworker.ts index f6d26ea6..e4ffdb4f 100644 --- a/src/lib/mtproto/mtprotoworker.ts +++ b/src/lib/mtproto/mtprotoworker.ts @@ -188,6 +188,10 @@ export class ApiManagerProxy extends CryptoWorkerMethods { this.performTaskWorkerVoid('setLanguage', language); }); + window.addEventListener('online', (event) => { + this.postMessage({type: 'online'}); + }); + /// #if !MTPROTO_SW this.registerWorker(); /// #endif diff --git a/src/lib/mtproto/networker.ts b/src/lib/mtproto/networker.ts index e8e8425e..dc1c298c 100644 --- a/src/lib/mtproto/networker.ts +++ b/src/lib/mtproto/networker.ts @@ -374,6 +374,12 @@ export default class MTPNetworker { (this.transport as TcpObfuscated).destroy(); } + public forceReconnectTimeout() { + if((this.transport as TcpObfuscated).reconnect) { + (this.transport as TcpObfuscated).reconnect(); + } + } + // private sendPingDelayDisconnect = () => { // if(this.pingPromise || true) return; diff --git a/src/lib/mtproto/networkerFactory.ts b/src/lib/mtproto/networkerFactory.ts index 6ae005e8..e16cd415 100644 --- a/src/lib/mtproto/networkerFactory.ts +++ b/src/lib/mtproto/networkerFactory.ts @@ -66,6 +66,12 @@ export class NetworkerFactory { } } } + + public forceReconnectTimeout() { + for(const networker of this.networkers) { + networker.forceReconnectTimeout(); + } + } } const networkerFactory = new NetworkerFactory(); diff --git a/src/lib/mtproto/transports/tcpObfuscated.ts b/src/lib/mtproto/transports/tcpObfuscated.ts index bfdb2244..44392f59 100644 --- a/src/lib/mtproto/transports/tcpObfuscated.ts +++ b/src/lib/mtproto/transports/tcpObfuscated.ts @@ -111,12 +111,7 @@ export default class TcpObfuscated implements MTTransport { }; private onClose = () => { - this.connected = false; - - this.connection.removeEventListener('open', this.onOpen); - this.connection.removeEventListener('close', this.onClose); - this.connection.removeEventListener('message', this.onMessage); - this.connection = undefined; + this.clear(); let needTimeout: number; if(this.autoReconnect) { @@ -138,6 +133,17 @@ export default class TcpObfuscated implements MTTransport { } }; + private clear() { + this.connected = false; + + if(this.connection) { + this.connection.removeEventListener('open', this.onOpen); + this.connection.removeEventListener('close', this.onClose); + this.connection.removeEventListener('message', this.onMessage); + this.connection = undefined; + } + } + /** * invoke only when closed */ @@ -147,7 +153,7 @@ export default class TcpObfuscated implements MTTransport { this.reconnectTimeout = undefined; } - if(this.connected) { + if(this.connection) { return; } @@ -194,6 +200,11 @@ export default class TcpObfuscated implements MTTransport { } private connect() { + if(this.connection) { + this.close(); + this.clear(); + } + this.connection = new this.Connection(this.dcId, this.url, this.logSuffix); this.connection.addEventListener('open', this.onOpen); this.connection.addEventListener('close', this.onClose);