From ad32bdab4485a0f6bf82629a2f93be2e0db86272 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Fri, 30 Oct 2020 16:02:09 +0700 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC=D1=8B=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D0=B8=D1=85=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B2=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D1=85=20=D0=BF=D0=BE=D0=BF=D1=8B=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=BF=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=20WebSocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/api/webSocketConnection.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/client/api/webSocketConnection.js b/client/api/webSocketConnection.js index bc052252..a2aefd52 100644 --- a/client/api/webSocketConnection.js +++ b/client/api/webSocketConnection.js @@ -1,3 +1,5 @@ +import * as utils from '../share/utils'; + const cleanPeriod = 60*1000;//1 минута class WebSocketConnection { @@ -9,6 +11,8 @@ class WebSocketConnection { this.messageQueue = []; this.messageLifeTime = messageLifeTime; this.requestId = 0; + + this.connecting = false; } addListener(listener) { @@ -53,14 +57,22 @@ class WebSocketConnection { } open(url) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { (async() => { + //Ожидаем окончания процесса подключения, если open уже был вызван + let i = 0; + while (this.connecting && i < 200) {//10 сек + await utils.sleep(50); + i++; + } + if (i >= 200) + this.connecting = false; + + //проверим подключение, и если нет, то подключимся заново if (this.ws && this.ws.readyState == WebSocket.OPEN) { resolve(this.ws); } else { - let protocol = 'ws:'; - if (window.location.protocol == 'https:') { - protocol = 'wss:' - } + this.connecting = true; + const protocol = (window.location.protocol == 'https:' ? 'wss:' : 'ws:'); url = url || `${protocol}//${window.location.host}/ws`; @@ -72,10 +84,9 @@ console.log('new connection'); } this.timer = setTimeout(() => { this.periodicClean(); }, cleanPeriod); - let resolved = false; this.ws.onopen = (e) => { console.log(this.ws.readyState); - resolved = true; + this.connecting = false; resolve(e); }; @@ -99,11 +110,13 @@ console.log(this.ws.readyState); this.ws.onerror = (e) => { this.emit(e.message, true); - if (!resolved) + if (this.connecting) { + this.connecting = false; reject(e); + } }; } - }); + })() }); } //timeout в минутах (cleanPeriod)