feather/src/utils/WebsocketClient.h

66 lines
2 KiB
C
Raw Normal View History

2021-05-02 18:22:38 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
2021-05-02 18:22:38 +00:00
#ifndef FEATHER_WEBSOCKETCLIENT_H
#define FEATHER_WEBSOCKETCLIENT_H
#include <QObject>
#include <QWebSocket>
#include <QTimer>
#include <QPointer>
2021-05-18 15:59:18 +00:00
#include "constants.h"
2023-02-11 17:11:21 +00:00
#include "utils/config.h"
2021-05-02 18:22:38 +00:00
class WebsocketClient : public QObject {
Q_OBJECT
public:
explicit WebsocketClient(QObject *parent = nullptr);
2022-03-11 15:03:06 +00:00
~WebsocketClient() override;
2021-05-02 18:22:38 +00:00
void start();
2022-03-04 21:55:29 +00:00
void restart();
void stop();
2021-05-02 18:22:38 +00:00
void sendMsg(const QByteArray &data);
2023-02-11 17:11:21 +00:00
void nextWebsocketUrl();
2021-05-02 18:22:38 +00:00
2023-03-01 12:28:01 +00:00
QWebSocket *webSocket;
2021-05-02 18:22:38 +00:00
signals:
void connectionEstablished();
void WSMessage(QJsonObject message);
private slots:
void onConnected();
2021-10-14 13:41:14 +00:00
void onDisconnected();
2022-02-23 23:42:06 +00:00
void onStateChanged(QAbstractSocket::SocketState state);
2021-05-02 18:22:38 +00:00
void onbinaryMessageReceived(const QByteArray &message);
void onError(QAbstractSocket::SocketError error);
2022-02-23 23:42:06 +00:00
void onConnectionTimeout();
2021-05-02 18:22:38 +00:00
private:
2023-02-11 17:11:21 +00:00
Config::Proxy networkType();
const QHash<Config::Proxy, QVector<QUrl>> m_websocketUrls = {
{Config::Proxy::None, {
QUrl(QStringLiteral("wss://ws.featherwallet.org/ws")),
QUrl(QStringLiteral("wss://ws.featherwallet.net/ws"))
}},
{Config::Proxy::Tor, {
QUrl(QStringLiteral("ws://7e6egbawekbkxzkv4244pqeqgoo4axko2imgjbedwnn6s5yb6b7oliqd.onion/ws")),
QUrl(QStringLiteral("ws://an5ecwgzyujqe7jverkp42d22zhvjes2mrhvol6tpqcgfkzwseqrafqd.onion/ws"))
}},
{Config::Proxy::i2p, {
QUrl(QStringLiteral("ws://hk5smvnkifjcm5346bs6cmnczwbiupr4jyiw3gz5z7ybaigp72fa.b32.i2p/ws")),
QUrl(QStringLiteral("ws://tr7iahturgfii64txw7cjhrfunmpg35w2lmmqmsa6i4jxwi7vplq.b32.i2p/ws"))
}}
};
2022-02-23 23:42:06 +00:00
QUrl m_url;
2021-05-02 18:22:38 +00:00
QTimer m_pingTimer;
2022-02-23 23:42:06 +00:00
QTimer m_connectionTimeout;
2023-01-14 13:55:44 +00:00
int m_timeout = 20;
2022-02-23 23:42:06 +00:00
int m_websocketUrlIndex = 0;
2022-03-04 21:55:29 +00:00
bool m_stopped = false;
2021-05-02 18:22:38 +00:00
};
#endif //FEATHER_WEBSOCKETCLIENT_H