Create keepalive

This commit is contained in:
Aditya Kulkarni 2019-02-11 09:43:44 -08:00
parent d5864a2e46
commit 4c8c8b3978
2 changed files with 17 additions and 0 deletions

View File

@ -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)

View File

@ -53,6 +53,7 @@ private:
MainWindow* parent = nullptr;
QWebSocket m_webSocket;
QString code;
QTimer* timer = nullptr;
};
enum NonceType {