Connect to WebSocket

This commit is contained in:
Pierre HUBERT 2020-06-13 11:28:08 +02:00
parent a0ce9f2d7f
commit e9d1bb6e61
5 changed files with 99 additions and 3 deletions

View File

@ -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 \

View File

@ -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)

View File

@ -6,6 +6,8 @@
#pragma once
#include "wsclient.h"
#include <QObject>
class RefreshService : public QObject
@ -22,4 +24,5 @@ private:
// Class members
static RefreshService *svc;
WsClient client;
};

47
wsclient.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <QTimer>
#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";
}

36
wsclient.h Normal file
View File

@ -0,0 +1,36 @@
/**
* WebSocket client
*
* @author Pierre Hubert
*/
#pragma once
#include "apiresponse.h"
#include <QObject>
#include <QWebSocket>
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;
};