From e9d1bb6e6139ca69a9311be2b8aefbeaf82d9394 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Jun 2020 11:28:08 +0200 Subject: [PATCH] Connect to WebSocket --- ComunicWatcher.pro | 8 +++++--- apirequest.cpp | 8 ++++++++ refreshservice.h | 3 +++ wsclient.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ wsclient.h | 36 +++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 wsclient.cpp create mode 100644 wsclient.h diff --git a/ComunicWatcher.pro b/ComunicWatcher.pro index a58e086..35d7924 100644 --- a/ComunicWatcher.pro +++ b/ComunicWatcher.pro @@ -1,4 +1,4 @@ -QT += core gui widgets network +QT += core gui widgets network websockets CONFIG += c++11 @@ -20,7 +20,8 @@ SOURCES += \ loginsuccessfuldialog.cpp \ main.cpp \ loginwindow.cpp \ - refreshservice.cpp + refreshservice.cpp \ + wsclient.cpp HEADERS += \ accounthelper.h \ @@ -29,7 +30,8 @@ HEADERS += \ config.h \ loginsuccessfuldialog.h \ loginwindow.h \ - refreshservice.h + refreshservice.h \ + wsclient.h FORMS += \ loginsuccessfuldialog.ui \ diff --git a/apirequest.cpp b/apirequest.cpp index fa68376..f4f1246 100644 --- a/apirequest.cpp +++ b/apirequest.cpp @@ -1,3 +1,4 @@ +#include "accounthelper.h" #include "apirequest.h" #include "config.h" @@ -10,6 +11,13 @@ APIRequest::APIRequest(const QString &uri, QObject *parent) : QObject(parent), m { addString("serviceName", API_SERVICE_NAME); addString("serviceToken", API_SERVICE_TOKEN); + + addString("icognito", "true"); + + if(AccountHelper::SignedIn()) { + addString("userToken1", AccountHelper::GetLoginToken()); + addString("userToken2", "dummy_data"); + } } void APIRequest::addString(const QString &name, const QString &value) diff --git a/refreshservice.h b/refreshservice.h index 730b451..aef201f 100644 --- a/refreshservice.h +++ b/refreshservice.h @@ -6,6 +6,8 @@ #pragma once +#include "wsclient.h" + #include class RefreshService : public QObject @@ -22,4 +24,5 @@ private: // Class members static RefreshService *svc; + WsClient client; }; diff --git a/wsclient.cpp b/wsclient.cpp new file mode 100644 index 0000000..ffb89a8 --- /dev/null +++ b/wsclient.cpp @@ -0,0 +1,47 @@ +#include + +#include "apirequest.h" +#include "wsclient.h" +#include "config.h" + +WsClient::WsClient(QObject *parent) : QObject(parent) +{ + startConnect(); + + connect(&mWebSocket, &QWebSocket::connected, this, &WsClient::onConnected); + connect(&mWebSocket, &QWebSocket::disconnected, this, &WsClient::onConnectionError); +} + +void WsClient::startConnect() +{ + // First, get an access token + APIRequest *r = new APIRequest("ws/token"); + r->exec(); + connect(r, &APIRequest::done, this, &WsClient::getTokenCallBack); +} + +void WsClient::onConnectionError() +{ + qWarning("Connection error, trying again in 30 seconds..."); + QTimer::singleShot(30000, this, &WsClient::startConnect); +} + +void WsClient::getTokenCallBack(APIResponse res) +{ + if(res.isError()) { + onConnectionError(); + return; + } + + // Determine WebSocket URL + const auto token = res.getObject().value("token").toString(); + const auto ws_url = QString(QString(API_SERVER_SCHEME) == "https" ? "wss" : "ws") + "://" + API_HOST + API_BASE_PATH + "ws?token=" + token; + qDebug() << "Connecting to " << ws_url; + + mWebSocket.open(ws_url); +} + +void WsClient::onConnected() +{ + qDebug() << "Connected to WebSocket"; +} diff --git a/wsclient.h b/wsclient.h new file mode 100644 index 0000000..74043de --- /dev/null +++ b/wsclient.h @@ -0,0 +1,36 @@ +/** + * WebSocket client + * + * @author Pierre Hubert + */ + +#pragma once + +#include "apiresponse.h" + +#include +#include + +class WsClient : public QObject +{ + Q_OBJECT +public: + explicit WsClient(QObject *parent = nullptr); + + +public slots: + void startConnect(); + +signals: + +private slots: + void onConnectionError(); + void getTokenCallBack(APIResponse res); + void onConnected(); + + +private: + // Class members + QWebSocket mWebSocket; +}; +