-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObsWebSocket.cpp
119 lines (107 loc) · 2.89 KB
/
ObsWebSocket.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
#include "ObsWebSocket.hpp"
#include <QJsonDocument>
#include <QtCore/QDebug>
QT_USE_NAMESPACE
ObsWebSocket::ObsWebSocket(const QUrl &url, bool debug, QObject *parent) :
QObject(parent),
m_url(url),
m_debug(debug)
{
startWebsocket();
}
void ObsWebSocket::setAddress(const QUrl &url)
{
if (m_url != url) {
m_url = url;
m_webSocket.close();
}
}
void ObsWebSocket::sendRequest(const QString &requestType, int msgId)
{
QJsonObject data {};
sendRequest(requestType, msgId, data);
}
void ObsWebSocket::sendRequest(const QString &requestType, int msgId, const QJsonObject &data)
{
QString messageId = QString::number(msgId);
QJsonObject object
{
{"request-type", requestType},
{"message-id", messageId}
};
foreach(const QString &key, data.keys()) {
QJsonValue value = data.value(key);
object[key] = value;
}
m_webSocket.sendTextMessage(jsonToString(object));
}
QString ObsWebSocket::jsonToString(const QJsonObject &json)
{
QJsonDocument Doc(json);
QByteArray ba = Doc.toJson();
return QString(ba);
}
QJsonObject ObsWebSocket::stringToJson(const QString &in)
{
QJsonObject obj;
QJsonDocument doc = QJsonDocument::fromJson(in.toUtf8());
if(!doc.isNull())
{
if(doc.isObject())
{
obj = doc.object();
}
else
{
if (m_debug)
qDebug() << "Document is not an object" << Qt::endl;
}
}
else
{
if (m_debug)
qDebug() << "Invalid JSON..." << Qt::endl;
}
return obj;
}
bool ObsWebSocket::isConnected()
{
return m_isConnected;
}
void ObsWebSocket::onConnected()
{
if (m_debug)
qDebug() << "WebSocket connected:" << m_url;
m_isConnected = true;
emit connected(m_isConnected);
connect(&m_webSocket, &QWebSocket::textMessageReceived,
this, &ObsWebSocket::onMessageReceived);
}
void ObsWebSocket::onClosed()
{
m_isConnected = false;
emit connected(m_isConnected);
disconnect(&m_webSocket, &QWebSocket::connected,
this, &ObsWebSocket::onConnected);
disconnect(&m_webSocket, &QWebSocket::disconnected,
this, &ObsWebSocket::onClosed);
disconnect(&m_webSocket, &QWebSocket::textMessageReceived,
this, &ObsWebSocket::onMessageReceived);
startWebsocket();
}
void ObsWebSocket::onMessageReceived(const QString &message)
{
emit onResponse(stringToJson(message));
if (m_debug)
qDebug().noquote() << "Message received:" << message;
}
void ObsWebSocket::startWebsocket()
{
if (m_debug)
qDebug() << "Connecting to WebSocket server:" << m_url;
connect(&m_webSocket, &QWebSocket::connected,
this, &ObsWebSocket::onConnected);
connect(&m_webSocket, &QWebSocket::disconnected,
this, &ObsWebSocket::onClosed);
m_webSocket.open(QUrl(m_url));
}