From e1da08570434b026083871e2314342b229e9168e Mon Sep 17 00:00:00 2001 From: 1aerostorm Date: Tue, 1 Oct 2024 16:43:19 +0300 Subject: [PATCH] HF 30 - Private groups - WebSocket ping --- src/components/pages/Messages.jsx | 15 ++++++++++++++- src/utils/NotifyApiClient.js | 17 ++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/components/pages/Messages.jsx b/src/components/pages/Messages.jsx index 9a4e7137..2e03e02a 100644 --- a/src/components/pages/Messages.jsx +++ b/src/components/pages/Messages.jsx @@ -32,7 +32,7 @@ import { getRoleInGroup, opGroup } from 'app/utils/groups' import { getProfileImage, } from 'app/utils/NormalizeProfile'; import { normalizeContacts, normalizeMessages } from 'app/utils/Normalizators'; import { fitToPreview } from 'app/utils/ImageUtils'; -import { notificationSubscribe, notificationSubscribeWs, +import { notificationSubscribe, notificationSubscribeWs, notifyWsPing, notificationShallowUnsubscribe, notificationTake, queueWatch, sendOffchainMessage } from 'app/utils/NotifyApiClient'; import { flash, unflash } from 'app/components/elements/messages/FlashTitle'; import { addShortcut } from 'app/utils/app/ShortcutUtils' @@ -329,6 +329,19 @@ class Messages extends React.Component { }, 5000) return } + const ping = async (firstCall = false) => { + if (!firstCall) { + try { + await notifyWsPing() + this.notifyErrorsClear() + } catch (err) { + console.error('Notify ping failed', err) + this.notifyErrorsInc(10) + } + } + setTimeout(ping, 10000) + } + ping(true) } componentDidMount() { diff --git a/src/utils/NotifyApiClient.js b/src/utils/NotifyApiClient.js index d998273c..63b95732 100644 --- a/src/utils/NotifyApiClient.js +++ b/src/utils/NotifyApiClient.js @@ -62,19 +62,10 @@ async function connectNotifyWs() { notifyWs.addEventListener('open', () => { notifyWs.isOpen = true clearTimeout(timeout) - setInterval(() => { - console.log('NOTW', notifyWs.readyState) - //notifyWs.send('_heartbeat') - }, 1000) resolve() }) - notifyWs.addEventListener('error', () => { - alert('NOTW close') - }) - notifyWs.addEventListener('сlose', () => { - alert('NOTW close') if (!notifyWs.isOpen) { clearTimeout(timeout) const err = new Error('notifyWs - cannot connect') @@ -138,6 +129,14 @@ async function notifyWsSend(api, args, callback = null, eventCallback = null) { } } +export async function notifyWsPing() { + await connectNotifyWs() + if (!window.notifyWs || window.notifyWs.readyState !== 1) { + throw new Error('Ping detected what Notify WS not ready') + } + window.notifyWs.send('_heartbeat') +} + export function notifyApiLogin(account, authSession) { if (!notifyAvailable()) return; let request = Object.assign({}, request_base, {