-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractHandler.cpp
74 lines (57 loc) · 1.56 KB
/
AbstractHandler.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
#include <httq/AbstractHandler.h>
#include <httq/HttpRequest.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QByteArray>
#include <QString>
#include <QWebSocket>
namespace httq
{
/// AbstractWebSocketHandler
AbstractWebSocketHandler::AbstractWebSocketHandler(QWebSocket *ws)
: QObject(nullptr) // TODO: parent ?
{
mWs = ws;
connect(ws, &QWebSocket::textMessageReceived,
this, &AbstractWebSocketHandler::_handleMessage);
connect(ws, &QWebSocket::disconnected,
ws, &QWebSocket::deleteLater);
connect(ws, &QWebSocket::destroyed,
this, &AbstractWebSocketHandler::deleteLater);
// QTimer *t = new QTimer();
// connect(t, &QTimer::timeout,
// this, [this]()
// {
// qWarning() << "I'm still alive!" << this->metaObject()->className();
// });
// t->setInterval(10000);
// t->start();
}
/// Abstract(Http)Handler
void AbstractHandler::answer(int status)
{
answer(status, QJsonObject());
}
void AbstractHandler::answer(int status, const QJsonObject &json)
{
assert(!mAlreadyAnswered);
mRequest->write(status, json);
deleteLater();
}
void AbstractHandler::answer(int status, const QJsonArray &json)
{
assert(!mAlreadyAnswered);
mRequest->write(status, json);
deleteLater();
}
void AbstractHandler::answer(int status, const QByteArray &content, const QString &contentType) // main
{
assert(!mAlreadyAnswered);
mRequest->write(status, content, contentType);
deleteLater();
}
void AbstractHandler::answer(int status, const QString &msg)
{
answer(status, msg.toUtf8(), "text/plain");
}
}