From a1836a64b281a767cdf5141669a12f1d0b0f91cc Mon Sep 17 00:00:00 2001 From: tobtoht Date: Tue, 1 Oct 2024 16:36:47 +0200 Subject: [PATCH] websocket: cleanup before quit --- src/utils/WebsocketClient.cpp | 6 +++++- src/utils/WebsocketNotifier.cpp | 6 +++++- src/utils/WebsocketNotifier.h | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/utils/WebsocketClient.cpp b/src/utils/WebsocketClient.cpp index 63f0e1d..85b6030 100644 --- a/src/utils/WebsocketClient.cpp +++ b/src/utils/WebsocketClient.cpp @@ -12,6 +12,8 @@ WebsocketClient::WebsocketClient(QObject *parent) : QObject(parent) , webSocket(new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this)) { + connect(qApp, &QCoreApplication::aboutToQuit, this, &WebsocketClient::stop); + connect(webSocket, &QWebSocket::stateChanged, this, &WebsocketClient::onStateChanged); connect(webSocket, &QWebSocket::connected, this, &WebsocketClient::onConnected); connect(webSocket, &QWebSocket::disconnected, this, &WebsocketClient::onDisconnected); @@ -26,7 +28,6 @@ WebsocketClient::WebsocketClient(QObject *parent) } }); m_pingTimer.setInterval(30 * 1000); - m_pingTimer.start(); connect(&m_connectionTimeout, &QTimer::timeout, this, &WebsocketClient::onConnectionTimeout); @@ -67,9 +68,11 @@ void WebsocketClient::restart() { } void WebsocketClient::stop() { + qDebug() << Q_FUNC_INFO; m_stopped = true; webSocket->close(); m_connectionTimeout.stop(); + m_pingTimer.stop(); } void WebsocketClient::onConnected() { @@ -89,6 +92,7 @@ void WebsocketClient::onStateChanged(QAbstractSocket::SocketState state) { } else if (state == QAbstractSocket::ConnectedState) { m_connectionTimeout.stop(); + m_pingTimer.start(); } } diff --git a/src/utils/WebsocketNotifier.cpp b/src/utils/WebsocketNotifier.cpp index 75666cc..b3fbc22 100644 --- a/src/utils/WebsocketNotifier.cpp +++ b/src/utils/WebsocketNotifier.cpp @@ -116,4 +116,8 @@ WebsocketNotifier* WebsocketNotifier::instance() } return m_instance; -} \ No newline at end of file +} + +WebsocketNotifier::~WebsocketNotifier() { + qDebug() << "~WebsocketNotifier" << QThread::currentThreadId(); +} diff --git a/src/utils/WebsocketNotifier.h b/src/utils/WebsocketNotifier.h index ea163fb..f2ee464 100644 --- a/src/utils/WebsocketNotifier.h +++ b/src/utils/WebsocketNotifier.h @@ -18,6 +18,7 @@ class WebsocketNotifier : public QObject { public: explicit WebsocketNotifier(QObject *parent); + ~WebsocketNotifier(); QMap heights; WebsocketClient *websocketClient;