summaryrefslogtreecommitdiffstats
path: root/app/qafbwsclient.h
blob: 2576ecbc5638b08854466dd7d441f00febb0e615 (plain)
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
#ifndef QAFBWSCLIENT_H
#define QAFBWSCLIENT_H

#include <QObject>
#include <QMap>
#include <QSharedPointer>
#include <QWebSocket>
#include <QJsonValue>

enum class AfMsgType
    : int
{
    Call = 2,
    RetOk = 3,
    RetErr = 4,
    Event = 5
};

class QAfbWsMsg;

class QAfbWsClient
    : public QObject
{
    Q_OBJECT
public:
    explicit QAfbWsClient(QObject* parent = nullptr);

    void open(const QUrl& url);
    void close();
    QUrl url() const;
    QSharedPointer<QAfbWsMsg> call(const QString &api, const QString &verb, const QJsonValue &args = QJsonValue());

signals:
    void connected();
    void disconnected();
    void error(QAbstractSocket::SocketError err, QString errStr);

private slots:
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketError(QAbstractSocket::SocketError err);
    void onSocketTextMessageReceived(QString msg);

private:
    int m_nextCallId;
    QWebSocket m_socket;
    QMap<int, QSharedPointer<QAfbWsMsg>> m_calls;
};

#endif // QAFBWSCLIENT_H