diff --git a/src/websockets.cpp b/src/websockets.cpp index a2ec647..3230088 100644 --- a/src/websockets.cpp +++ b/src/websockets.cpp @@ -82,6 +82,9 @@ WormholeClient::~WormholeClient() { if (m_webSocket.isValid()) { m_webSocket.close(); } + + timer->stop(); + delete timer; } void WormholeClient::connect() { @@ -103,6 +106,19 @@ void WormholeClient::onConnected() }).toJson(); m_webSocket.sendTextMessage(payload); + + // On connected, we'll also create a timer to ping it every 4 minutes, since the websocket + // will timeout after 5 minutes + timer = new QTimer(parent); + QObject::connect(timer, &QTimer::timeout, [=]() { + if (m_webSocket.isValid()) { + auto payload = QJsonDocument(QJsonObject { + {"ping", "ping"} + }).toJson(); + m_webSocket.sendTextMessage(payload); + } + }); + timer->start(4 * 60 * 1000); // 4 minutes } void WormholeClient::onTextMessageReceived(QString message) diff --git a/src/websockets.h b/src/websockets.h index 8418e09..8b49ef6 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -53,6 +53,7 @@ private: MainWindow* parent = nullptr; QWebSocket m_webSocket; QString code; + QTimer* timer = nullptr; }; enum NonceType {