Commit 24dc8b2f authored by Leander Schulten's avatar Leander Schulten

RemoteVolume: Try to reconnect after connection lost. Log connection abort.

parent 18b8e582
Pipeline #206256 passed with stage
in 4 minutes and 28 seconds
#include "remotevolume.h"
#include "systemvolume.h"
#include <QTimerEvent>
#include <QtDebug>
RemoteVolume::RemoteVolume(Settings &settings) : settings(settings) {
QObject::connect(&settings, &Settings::computerNameChanged, [this]() {
......@@ -14,12 +16,33 @@ RemoteVolume::RemoteVolume(Settings &settings) : settings(settings) {
}
} else {
webSocket.close();
// if we were in reconnection mode, stop the timer
if (reconnectTimerId >= 0) {
killTimer(reconnectTimerId);
reconnectTimerId = -1;
}
}
});
QObject::connect(&webSocket, qOverload<QAbstractSocket::SocketError>(&QWebSocket::error), [this](const auto error) { qWarning() << "Remote Volume: WebSocket connection error: " << error << " : " << webSocket.errorString(); });
QObject::connect(&webSocket, &QWebSocket::disconnected, [this]() {
emit isConnectedChanged();
// if we lost the connection, but don't want that, wait some time and reconnect then
if (this->settings.remoteVolumeControl()) {
if (reconnectTimerId == -1) {
startTimer(WAIT_FOR_RECONNECT_MS);
}
}
});
QObject::connect(&webSocket, &QWebSocket::connected, [this]() {
webSocket.sendTextMessage("Name:" + this->settings.getComputerName());
webSocket.sendTextMessage("Value:" + QString::number(SystemVolume::get().getVolume()));
emit isConnectedChanged();
// if we were in reconnection mode, stop the timer
if (reconnectTimerId >= 0) {
killTimer(reconnectTimerId);
reconnectTimerId = -1;
}
});
QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](const QString &message) {
bool ok;
......@@ -41,3 +64,10 @@ RemoteVolume::RemoteVolume(Settings &settings) : settings(settings) {
void RemoteVolume::connect() {
webSocket.open(QUrl(QStringLiteral("wss://orga.symposion.hilton.rwth-aachen.de/volumeClient")));
}
void RemoteVolume::timerEvent(QTimerEvent *event) {
if (event->timerId() == reconnectTimerId) {
event->accept();
connect();
}
}
#ifndef REMOTEVOLUME_H
#define REMOTEVOLUME_H
#include <QObject>
#include <QWebSocket>
#include <settings.h>
class RemoteVolume
{
class RemoteVolume : public QObject {
Q_OBJECT
QWebSocket webSocket;
Settings &settings;
int reconnectTimerId = -1;
static constexpr int WAIT_FOR_RECONNECT_MS = 5 * 1000;
Q_PROPERTY(bool isConnected READ isConnected NOTIFY isConnectedChanged)
public:
RemoteVolume(Settings &settings);
explicit RemoteVolume(Settings &settings);
void connect();
[[nodiscard]] bool isConnected() const { return webSocket.isValid(); }
protected:
void timerEvent(QTimerEvent *event) override;
signals:
void isConnectedChanged();
};
#endif // REMOTEVOLUME_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment