From 41c802495641457d0ed63fcc56e3be0a9116d102 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Sat, 2 Apr 2022 15:02:53 +0300 Subject: [PATCH] Transport detection: close pending websockets --- src/lib/mtproto/transports/controller.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/mtproto/transports/controller.ts b/src/lib/mtproto/transports/controller.ts index 02acced0..b39a358a 100644 --- a/src/lib/mtproto/transports/controller.ts +++ b/src/lib/mtproto/transports/controller.ts @@ -57,7 +57,17 @@ export class MTTransportController extends EventListenerBase<{ socket.setAutoReconnect(false); socket.connection.addEventListener('close', () => websocketPromise.resolve(false), {once: true}); socket.connection.addEventListener('open', () => websocketPromise.resolve(true), {once: true}); - setTimeout(() => websocketPromise.resolve(false), timeout); + setTimeout(() => { + if(websocketPromise.isFulfilled || websocketPromise.isRejected) { + return; + } + + if(socket.connection) { + socket.connection.close(); + } + + websocketPromise.resolve(false); + }, timeout); const [isHttpAvailable, isWebSocketAvailable] = await Promise.all([httpPromise, websocketPromise]);