Add retry logic
This commit is contained in:
@@ -79,8 +79,10 @@ WormholeClient::WormholeClient(MainWindow* p, QString wormholeCode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
WormholeClient::~WormholeClient() {
|
WormholeClient::~WormholeClient() {
|
||||||
if (m_webSocket.isValid()) {
|
shuttingDown = true;
|
||||||
m_webSocket.close();
|
|
||||||
|
if (m_webSocket->isValid()) {
|
||||||
|
m_webSocket->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
timer->stop();
|
timer->stop();
|
||||||
@@ -88,34 +90,59 @@ WormholeClient::~WormholeClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WormholeClient::connect() {
|
void WormholeClient::connect() {
|
||||||
QObject::connect(&m_webSocket, &QWebSocket::connected, this, &WormholeClient::onConnected);
|
delete m_webSocket;
|
||||||
QObject::connect(&m_webSocket, &QWebSocket::disconnected, this, &WormholeClient::closed);
|
m_webSocket = new QWebSocket();
|
||||||
|
|
||||||
m_webSocket.open(QUrl("ws://127.0.0.1:7070"));
|
QObject::connect(m_webSocket, &QWebSocket::connected, this, &WormholeClient::onConnected);
|
||||||
|
QObject::connect(m_webSocket, &QWebSocket::disconnected, this, &WormholeClient::closed);
|
||||||
|
|
||||||
|
m_webSocket->open(QUrl("ws://127.0.0.1:7070"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WormholeClient::retryConnect() {
|
||||||
|
QTimer::singleShot(5 * 1000 * pow(2, retryCount), [=]() {
|
||||||
|
if (retryCount < 10) {
|
||||||
|
qDebug() << "Retrying websocket connection";
|
||||||
|
this->retryCount++;
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qDebug() << "Retry count exceeded, will not attempt retry any more";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called when the websocket is closed. If this was closed without our explicitly closing it,
|
||||||
|
// then we need to try and reconnect
|
||||||
|
void WormholeClient::closed() {
|
||||||
|
if (!shuttingDown) {
|
||||||
|
retryConnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WormholeClient::onConnected()
|
void WormholeClient::onConnected()
|
||||||
{
|
{
|
||||||
qDebug() << "WebSocket connected";
|
qDebug() << "WebSocket connected";
|
||||||
QObject::connect(&m_webSocket, &QWebSocket::textMessageReceived,
|
retryCount = 0;
|
||||||
|
|
||||||
|
QObject::connect(m_webSocket, &QWebSocket::textMessageReceived,
|
||||||
this, &WormholeClient::onTextMessageReceived);
|
this, &WormholeClient::onTextMessageReceived);
|
||||||
|
|
||||||
auto payload = QJsonDocument( QJsonObject {
|
auto payload = QJsonDocument( QJsonObject {
|
||||||
{"register", code}
|
{"register", code}
|
||||||
}).toJson();
|
}).toJson();
|
||||||
|
|
||||||
m_webSocket.sendTextMessage(payload);
|
m_webSocket->sendTextMessage(payload);
|
||||||
|
|
||||||
// On connected, we'll also create a timer to ping it every 4 minutes, since the websocket
|
// On connected, we'll also create a timer to ping it every 4 minutes, since the websocket
|
||||||
// will timeout after 5 minutes
|
// will timeout after 5 minutes
|
||||||
timer = new QTimer(parent);
|
timer = new QTimer(parent);
|
||||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||||
if (m_webSocket.isValid()) {
|
if (m_webSocket->isValid()) {
|
||||||
auto payload = QJsonDocument(QJsonObject {
|
auto payload = QJsonDocument(QJsonObject {
|
||||||
{"ping", "ping"}
|
{"ping", "ping"}
|
||||||
}).toJson();
|
}).toJson();
|
||||||
m_webSocket.sendTextMessage(payload);
|
m_webSocket->sendTextMessage(payload);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
timer->start(4 * 60 * 1000); // 4 minutes
|
timer->start(4 * 60 * 1000); // 4 minutes
|
||||||
@@ -124,7 +151,7 @@ void WormholeClient::onConnected()
|
|||||||
void WormholeClient::onTextMessageReceived(QString message)
|
void WormholeClient::onTextMessageReceived(QString message)
|
||||||
{
|
{
|
||||||
qDebug() << "Message received:" << message;
|
qDebug() << "Message received:" << message;
|
||||||
AppDataServer::getInstance()->processMessage(message, parent, &m_webSocket, AppConnectionType::INTERNET);
|
AppDataServer::getInstance()->processMessage(message, parent, m_webSocket, AppConnectionType::INTERNET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -36,24 +36,27 @@ private:
|
|||||||
class WormholeClient : public QObject {
|
class WormholeClient : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_SIGNALS:
|
|
||||||
void closed();
|
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void onConnected();
|
void onConnected();
|
||||||
void onTextMessageReceived(QString message);
|
void onTextMessageReceived(QString message);
|
||||||
|
void closed();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WormholeClient(MainWindow* parent, QString wormholeCode);
|
WormholeClient(MainWindow* parent, QString wormholeCode);
|
||||||
~WormholeClient();
|
~WormholeClient();
|
||||||
|
|
||||||
void connect();
|
void connect();
|
||||||
|
void retryConnect();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MainWindow* parent = nullptr;
|
MainWindow* parent = nullptr;
|
||||||
QWebSocket m_webSocket;
|
QWebSocket* m_webSocket = nullptr;
|
||||||
|
|
||||||
|
QTimer* timer = nullptr;
|
||||||
|
|
||||||
QString code;
|
QString code;
|
||||||
QTimer* timer = nullptr;
|
int retryCount = 0;
|
||||||
|
bool shuttingDown = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum NonceType {
|
enum NonceType {
|
||||||
|
|||||||
Reference in New Issue
Block a user