-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawebsocketserver.cpp
More file actions
138 lines (111 loc) · 3.65 KB
/
awebsocketserver.cpp
File metadata and controls
138 lines (111 loc) · 3.65 KB
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
129
130
131
132
133
134
135
136
137
138
#include "awebsocketserver.h"
#include <QWebSocketServer>
#include <QWebSocket>
#include <QDebug>
#include <QNetworkInterface>
#include <QJsonObject>
#include <QJsonDocument>
AWebSocketServer::AWebSocketServer(QObject *parent) :
QObject(parent),
server(new QWebSocketServer(QStringLiteral("Dispatcher for Ants2 server"), QWebSocketServer::NonSecureMode, this))
{
connect(server, &QWebSocketServer::newConnection, this, &AWebSocketServer::onNewConnection, Qt::QueuedConnection);
watchdog = new QTimer(this);
watchdog->setSingleShot(true);
watchdog->setInterval(watchdogInterval);
}
AWebSocketServer::~AWebSocketServer()
{
server->close();
server->deleteLater();
}
bool AWebSocketServer::StartListen(QHostAddress ip, quint16 port)
{
qDebug() << "Attempting to start listening...";
if ( !server->listen(ip, port) ) //QHostAddress::Any QHostAddress::AnyIPv4
{
QString s = QString("WebSocket server was unable to start listen on ip ") + ip.toString() + " and port " + QString::number(port);
qCritical(s.toLatin1().data());
exit(1);
}
qDebug() << "Dispatcher for ANTS2 server is listening.";
qDebug() << "To connect use URL:" << GetUrl();
qDebug() << "";
return true;
}
bool AWebSocketServer::CanReply()
{
if (client) return true;
qDebug() << "AWebSocketSessionServer: cannot reply - connection not established";
return false;
}
void AWebSocketServer::ReplyAndCloseConnection(const QJsonObject &json)
{
QJsonDocument doc(json);
QString reply(doc.toJson(QJsonDocument::Compact));
ReplyAndCloseConnection(reply);
}
void AWebSocketServer::ReplyAndCloseConnection(const QString &message)
{
if ( !CanReply() ) return;
qDebug() << "Reply text message:"<<message;
client->sendTextMessage(message);
client->close(); //QWebSocketProtocol::CloseCodeNormal, "test");
}
void AWebSocketServer::onNewConnection()
{
qDebug() << "New connection attempt";
if (client)
{
//deny - exclusive connections!
qDebug() << "Connection delayed: another client is already connected";
//pSocket->sendTextMessage("{\"result\":false, \"error\":\"another session is not yet finished\"}");
//pSocket->close();
return;
}
client = server->nextPendingConnection();
ProcessNewClient();
}
void AWebSocketServer::ProcessNewClient()
{
qDebug() << "Connection established with" << client->peerAddress().toString();
connect(client, &QWebSocket::textMessageReceived, this, &AWebSocketServer::onTextMessageReceived);
connect(client, &QWebSocket::disconnected, this, &AWebSocketServer::onSocketDisconnected);
client->sendTextMessage("{\"result\":true}");
watchdog->start();
}
void AWebSocketServer::onTextMessageReceived(const QString &message)
{
qDebug() << "Text message received:\n--->\n"<<message << "\n<---";
if (message.isEmpty())
{
if (client)
client->sendTextMessage("PONG"); //used for watchdogs
}
else
emit textMessageReceived(message);
}
void AWebSocketServer::onSocketDisconnected()
{
qDebug() << "Client disconnected!";
if (client)
{
client->close();
client->deleteLater();
}
client = 0;
client = server->nextPendingConnection();
if (client) ProcessNewClient();
}
void AWebSocketServer::onWatchdogTriggered()
{
onSocketDisconnected();
}
const QString AWebSocketServer::GetUrl() const
{
return server->serverUrl().toString();
}
int AWebSocketServer::GetPort() const
{
return server->serverPort();
}