]> Nutra Git (v2) - gamesguru/feather.git/commitdiff
Websocket: add fallback
authortobtoht <thotbot@protonmail.com>
Wed, 23 Feb 2022 23:42:06 +0000 (00:42 +0100)
committertobtoht <thotbot@protonmail.com>
Wed, 23 Feb 2022 23:42:06 +0000 (00:42 +0100)
src/constants.h
src/utils/WebsocketClient.cpp
src/utils/WebsocketClient.h

index 73d9edff8dfb5734cbdc298be39480e29c54c10f..9b2794b154ea93a7bc412cb7560d652f4d1601c4 100644 (file)
@@ -27,7 +27,10 @@ namespace constants
     const int donationBoundary = 25;
 
     // websocket constants
-    const QUrl websocketUrl = QUrl(QStringLiteral("ws://7e6egbawekbkxzkv4244pqeqgoo4axko2imgjbedwnn6s5yb6b7oliqd.onion/ws"));
+    const QVector<QUrl> websocketUrls = {
+        QUrl(QStringLiteral("ws://7e6egbawekbkxzkv4244pqeqgoo4axko2imgjbedwnn6s5yb6b7oliqd.onion/ws")),
+        QUrl(QStringLiteral("ws://an5ecwgzyujqe7jverkp42d22zhvjes2mrhvol6tpqcgfkzwseqrafqd.onion/ws"))
+    };
 
     // website constants
     const QString websiteUrl = "https://featherwallet.org";
index 7168d81ecbe158095216bb47b7e669839d24e8e6..ff915e3f36ebd65e0f9d928b155d41104c0bc627 100644 (file)
@@ -9,6 +9,7 @@
 WebsocketClient::WebsocketClient(QObject *parent)
     : QObject(parent)
 {
+    connect(&webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged);
     connect(&webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected);
     connect(&webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected);
     connect(&webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebsocketClient::onError);
@@ -23,6 +24,11 @@ WebsocketClient::WebsocketClient(QObject *parent)
     });
     m_pingTimer.setInterval(30 * 1000);
     m_pingTimer.start();
+
+    connect(&m_connectionTimeout, &QTimer::timeout, this, &WebsocketClient::onConnectionTimeout);
+
+    m_websocketUrlIndex = QRandomGenerator::global()->bounded(constants::websocketUrls.length());
+    this->nextWebsocketUrl();
 }
 
 void WebsocketClient::sendMsg(const QByteArray &data) {
@@ -48,9 +54,19 @@ void WebsocketClient::onConnected() {
 
 void WebsocketClient::onDisconnected() {
     qDebug() << "WebSocket disconnected";
+    this->nextWebsocketUrl();
     QTimer::singleShot(1000, [this]{this->start();});
 }
 
+void WebsocketClient::onStateChanged(QAbstractSocket::SocketState state) {
+    if (state == QAbstractSocket::ConnectingState) {
+        m_connectionTimeout.start(m_timeout*1000);
+    }
+    else if (state == QAbstractSocket::ConnectedState) {
+        m_connectionTimeout.stop();
+    }
+}
+
 void WebsocketClient::onError(QAbstractSocket::SocketError error) {
     qCritical() << "WebSocket error: " << error;
     auto state = webSocket.state();
@@ -59,8 +75,19 @@ void WebsocketClient::onError(QAbstractSocket::SocketError error) {
     }
 }
 
+void WebsocketClient::nextWebsocketUrl() {
+    m_url = constants::websocketUrls[m_websocketUrlIndex];
+    m_websocketUrlIndex = (m_websocketUrlIndex+1)%constants::websocketUrls.length();
+}
+
+void WebsocketClient::onConnectionTimeout() {
+    qWarning() << "Websocket connection timeout";
+    m_timeout = std::min(m_timeout + 5, 60);
+    this->onDisconnected();
+}
+
 void WebsocketClient::onbinaryMessageReceived(const QByteArray &message) {
-    qDebug() << "WebSocket received:" << message;
+//    qDebug() << "WebSocket received:" << message;
 
     if (!Utils::validateJSON(message)) {
         qCritical() << "Could not interpret WebSocket message as JSON";
index ae97da5713d0955ef3e83f37084b0dda7945a38f..d2479a34d8b5470067ff3f6769c2758091880b41 100644 (file)
@@ -27,12 +27,18 @@ signals:
 private slots:
     void onConnected();
     void onDisconnected();
+    void onStateChanged(QAbstractSocket::SocketState state);
     void onbinaryMessageReceived(const QByteArray &message);
     void onError(QAbstractSocket::SocketError error);
+    void nextWebsocketUrl();
+    void onConnectionTimeout();
 
 private:
-    QUrl m_url = constants::websocketUrl;
+    QUrl m_url;
     QTimer m_pingTimer;
+    QTimer m_connectionTimeout;
+    int m_timeout = 10;
+    int m_websocketUrlIndex = 0;
 };
 
 #endif //FEATHER_WEBSOCKETCLIENT_H