-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsclient.cpp
128 lines (108 loc) · 3.6 KB
/
wsclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "wsclient.h"
#include "qmessagebox.h"
#include <QtWebSockets/QWebSocket>
WsClient::WsClient(QSettings *settings, QObject *parent) : QObject(parent), settings(settings)
{
authStatus = false;
qDebug()<<"SSL version use for build: "<<QSslSocket::sslLibraryBuildVersionString();
}
void WsClient::sendTextMessage(QString message)
{
websocket->sendTextMessage(message);
}
void WsClient::onClosed()
{
qDebug() << "Client disconnected";
authStatus = false;
}
void WsClient::onConnected()
{
QString clientName = settings->value(Constants::clientNameConfigName(), "").toString();
QString clientKey = settings->value(Constants::clientKeyConfigName(), "").toString();
qDebug() << "Client connected" << clientName << clientKey;
authorization(clientName, clientKey);
}
void WsClient::onTextMessageReceived(QString message)
{
qDebug() << "Message received: " << message;
QJsonDocument jsonMessage = QJsonDocument::fromJson(message.toUtf8());
MessageType type = unregisterMessage(jsonMessage.object().value("id").toInt());
switch (type) {
case AUTH_MSG:
qDebug() << "Auth response";
// set auth
authStatus = jsonMessage.object().value("status").toString() == "accepted";
onAuthResponse(jsonMessage);
break;
case TREE_MSG:
qDebug() << "Tree response";
onTreeChange(jsonMessage);
break;
case COPY_MSG:
qDebug() << "Copy response";
onCopyMessage(jsonMessage);
break;
default:
break;
}
}
int WsClient::generateMessageId()
{
return (int) QDateTime::currentMSecsSinceEpoch();
}
void WsClient::registerMessage(int id, MessageType type)
{
messages[id] = type;
}
MessageType WsClient::unregisterMessage(int id)
{
if (messages.contains(id)) {
return messages[id];
}
return MessageType::UNKOWN;
}
void WsClient::getTree()
{
if (authStatus) {
qDebug() << "Get tree from server";
int id = generateMessageId();
registerMessage(id, TREE_MSG);
sendTextMessage(QString("{\"id\": %1,\"type\":\"TreeMsg\"}").arg(QString::number(id)));
}
}
int WsClient::copyRequest(int start, int end, QString hash)
{
if (authStatus) {
qDebug() << "Copy request message";
int id = generateMessageId();
registerMessage(id, COPY_MSG);
QString cpyMsg = QString("{\"type\":\"CopyMsg\", \"id\": %1, \"start\": %2, \"end\": %3, \"file_hash\": \"%4\"}")
.arg(QString::number(id), QString::number(start), QString::number(end), hash);
sendTextMessage(cpyMsg);
return id;
}
return -1;
}
void WsClient::authorization(QString name, QString password)
{
qDebug() << "Authorization message";
int id = generateMessageId();
registerMessage(id, AUTH_MSG);
sendTextMessage(QString("{\"id\": %1, \"name\": \"%2\", \"key\":\"%3\", \"type\":\"AuthMsg\"}").arg(QString::number(id), name, password));
}
bool WsClient::connectToServer()
{
QString domain = settings->value(Constants::domainConfigName(), "").toString();
qDebug() << "Server url: " << domain;
if (domain != "") {
websocket = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest);
connect(websocket, &QWebSocket::connected, this, &WsClient::onConnected);
connect(websocket, &QWebSocket::disconnected, this, &WsClient::onClosed);
connect(websocket, &QWebSocket::textMessageReceived, this, &WsClient::onTextMessageReceived);
websocket->open(QUrl(domain));
} else {
QMessageBox msgBox;
msgBox.setText("Invalid server url.");
msgBox.exec();
}
}