mirror of
https://github.com/feather-wallet/feather.git
synced 2024-11-16 17:27:38 +00:00
Websocket: fix unnecessary reconnect
This commit is contained in:
parent
baf4fdc67d
commit
3ef10e9b53
3 changed files with 18 additions and 43 deletions
|
@ -460,8 +460,6 @@ void WindowManager::initTor() {
|
||||||
torManager()->init();
|
torManager()->init();
|
||||||
torManager()->start();
|
torManager()->start();
|
||||||
|
|
||||||
connect(torManager(), &TorManager::connectionStateChanged, &websocketNotifier()->websocketClient, &WebsocketClient::onToggleConnect);
|
|
||||||
|
|
||||||
this->onTorSettingsChanged();
|
this->onTorSettingsChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,76 +9,59 @@
|
||||||
WebsocketClient::WebsocketClient(QObject *parent)
|
WebsocketClient::WebsocketClient(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
{
|
{
|
||||||
connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived);
|
|
||||||
connect(&webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected);
|
connect(&webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected);
|
||||||
connect(&webSocket, &QWebSocket::disconnected, this, &WebsocketClient::closed);
|
connect(&webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected);
|
||||||
connect(&webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebsocketClient::onError);
|
connect(&webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &WebsocketClient::onError);
|
||||||
connect(&m_connectionTimer, &QTimer::timeout, this, &WebsocketClient::checkConnection);
|
|
||||||
|
connect(&webSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketClient::onbinaryMessageReceived);
|
||||||
|
|
||||||
// Keep websocket connection alive
|
// Keep websocket connection alive
|
||||||
connect(&m_pingTimer, &QTimer::timeout, [this]{
|
connect(&m_pingTimer, &QTimer::timeout, [this]{
|
||||||
if (webSocket.state() == QAbstractSocket::ConnectedState)
|
if (webSocket.state() == QAbstractSocket::ConnectedState) {
|
||||||
webSocket.ping();
|
webSocket.ping();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
m_pingTimer.setInterval(30 * 1000);
|
m_pingTimer.setInterval(30 * 1000);
|
||||||
m_pingTimer.start();
|
m_pingTimer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebsocketClient::sendMsg(const QByteArray &data) {
|
void WebsocketClient::sendMsg(const QByteArray &data) {
|
||||||
if (webSocket.state() == QAbstractSocket::ConnectedState)
|
if (webSocket.state() == QAbstractSocket::ConnectedState) {
|
||||||
webSocket.sendBinaryMessage(data);
|
webSocket.sendBinaryMessage(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebsocketClient::onToggleConnect(bool connect) {
|
|
||||||
m_connect = connect;
|
|
||||||
if (m_connect)
|
|
||||||
checkConnection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebsocketClient::start() {
|
void WebsocketClient::start() {
|
||||||
// connect & reconnect on errors/close
|
// connect & reconnect on errors/close
|
||||||
#ifdef QT_DEBUG
|
|
||||||
qDebug() << "WebSocket connect:" << m_url.url();
|
qDebug() << "WebSocket connect:" << m_url.url();
|
||||||
#endif
|
|
||||||
|
|
||||||
if (m_connect)
|
auto state = webSocket.state();
|
||||||
|
if (state != QAbstractSocket::ConnectedState && state != QAbstractSocket::ConnectingState) {
|
||||||
webSocket.open(m_url);
|
webSocket.open(m_url);
|
||||||
|
|
||||||
if (!m_connectionTimer.isActive()) {
|
|
||||||
m_connectionTimer.start(2000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WebsocketClient::checkConnection() {
|
|
||||||
if (!m_connect)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (webSocket.state() == QAbstractSocket::UnconnectedState) {
|
|
||||||
#ifdef QT_DEBUG
|
|
||||||
qDebug() << "WebSocket reconnect";
|
|
||||||
#endif
|
|
||||||
this->start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebsocketClient::onConnected() {
|
void WebsocketClient::onConnected() {
|
||||||
#ifdef QT_DEBUG
|
|
||||||
qDebug() << "WebSocket connected";
|
qDebug() << "WebSocket connected";
|
||||||
#endif
|
|
||||||
emit connectionEstablished();
|
emit connectionEstablished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebsocketClient::onDisconnected() {
|
||||||
|
qDebug() << "WebSocket disconnected";
|
||||||
|
QTimer::singleShot(1000, [this]{this->start();});
|
||||||
|
}
|
||||||
|
|
||||||
void WebsocketClient::onError(QAbstractSocket::SocketError error) {
|
void WebsocketClient::onError(QAbstractSocket::SocketError error) {
|
||||||
qCritical() << "WebSocket error: " << error;
|
qCritical() << "WebSocket error: " << error;
|
||||||
auto state = webSocket.state();
|
auto state = webSocket.state();
|
||||||
if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState)
|
if (state == QAbstractSocket::ConnectedState || state == QAbstractSocket::ConnectingState) {
|
||||||
webSocket.abort();
|
webSocket.abort();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebsocketClient::onbinaryMessageReceived(const QByteArray &message) {
|
void WebsocketClient::onbinaryMessageReceived(const QByteArray &message) {
|
||||||
#ifdef QT_DEBUG
|
|
||||||
qDebug() << "WebSocket received:" << message;
|
qDebug() << "WebSocket received:" << message;
|
||||||
#endif
|
|
||||||
if (!Utils::validateJSON(message)) {
|
if (!Utils::validateJSON(message)) {
|
||||||
qCritical() << "Could not interpret WebSocket message as JSON";
|
qCritical() << "Could not interpret WebSocket message as JSON";
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -20,24 +20,18 @@ public:
|
||||||
|
|
||||||
QWebSocket webSocket;
|
QWebSocket webSocket;
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onToggleConnect(bool connect);
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void closed();
|
|
||||||
void connectionEstablished();
|
void connectionEstablished();
|
||||||
void WSMessage(QJsonObject message);
|
void WSMessage(QJsonObject message);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onConnected();
|
void onConnected();
|
||||||
|
void onDisconnected();
|
||||||
void onbinaryMessageReceived(const QByteArray &message);
|
void onbinaryMessageReceived(const QByteArray &message);
|
||||||
void checkConnection();
|
|
||||||
void onError(QAbstractSocket::SocketError error);
|
void onError(QAbstractSocket::SocketError error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_connect = false;
|
|
||||||
QUrl m_url = constants::websocketUrl;
|
QUrl m_url = constants::websocketUrl;
|
||||||
QTimer m_connectionTimer;
|
|
||||||
QTimer m_pingTimer;
|
QTimer m_pingTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue