From 41a0fb3b1083afa3b9042586c7f9488dcd4eff82 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Tue, 22 Dec 2020 23:46:01 +0100 Subject: [PATCH] Websocket: keep connection alive --- src/utils/wsclient.cpp | 8 ++++++++ src/utils/wsclient.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/utils/wsclient.cpp b/src/utils/wsclient.cpp index fa3f690..78cac69 100644 --- a/src/utils/wsclient.cpp +++ b/src/utils/wsclient.cpp @@ -21,6 +21,14 @@ WSClient::WSClient(AppContext *ctx, const QUrl &url, QObject *parent) : connect(&this->webSocket, QOverload::of(&QWebSocket::error), this, &WSClient::onError); m_tor = url.host().endsWith(".onion"); + + // Keep websocket connection alive + connect(&m_pingTimer, &QTimer::timeout, [this]{ + if (this->webSocket.state() == QAbstractSocket::ConnectedState) + this->webSocket.ping(); + }); + m_pingTimer.setInterval(30 * 1000); + m_pingTimer.start(); } void WSClient::sendMsg(const QByteArray &data) { diff --git a/src/utils/wsclient.h b/src/utils/wsclient.h index 4e90298..57a9a93 100644 --- a/src/utils/wsclient.h +++ b/src/utils/wsclient.h @@ -33,6 +33,7 @@ private slots: private: QTimer m_connectionTimer; + QTimer m_pingTimer; AppContext *m_ctx; bool m_tor = true; };