-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcwebsocketclient.h
67 lines (52 loc) · 1.36 KB
/
rpcwebsocketclient.h
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
#pragma once
#include <QDebug>
#include <QObject>
#include <QUrl>
#include <QVariantMap>
#include <QtWebSockets/QWebSocket>
class RpcWebSocketClient : public QObject {
Q_OBJECT
public:
explicit RpcWebSocketClient(QObject *parent = nullptr);
void open(const QUrl &url);
struct Request {
QString method;
QVariantMap params;
QVariantMap toMap() const {
return {
{"method", method},
{"params", params},
};
}
};
struct Response {
QVariant result;
QVariant error;
QVariantMap toMap() const {
return {
{"result", result},
{"error", error},
};
}
};
using Callback = std::function<void(const Response &)>;
void invoke(const Request &request, Callback callback);
public slots:
void onTextMessageReceived(QString message);
signals:
void connected();
void disconnected();
private:
QUrl m_url;
QWebSocket m_websocket;
QMap<int, Callback> m_callbacks;
int m_invokeSequence = 0;
};
inline QDebug operator<<(QDebug debug, const RpcWebSocketClient::Request &request) {
debug << request.toMap();
return debug;
}
inline QDebug operator<<(QDebug debug, const RpcWebSocketClient::Response &response) {
debug << response.toMap();
return debug;
}