-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIWebsockets.cpp
More file actions
60 lines (50 loc) · 1.79 KB
/
APIWebsockets.cpp
File metadata and controls
60 lines (50 loc) · 1.79 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
#include "APIWebsockets.h"
#include "QtWebSockets/QWebSocketServer"
#include "QtWebSockets/QWebSocket"
#include <QtCore/QDebug>
#include <QWidget>
#include <iostream>
#include "MainWidget.h"
QT_USE_NAMESPACE
APIWebsockets::APIWebsockets(quint16 port, MainWidget *container, QObject *parent) :
QObject(parent),
m_pWebSocketServer(Q_NULLPTR),
m_client()
{
m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Potree API"),
QWebSocketServer::NonSecureMode,
this);
m_container = container;
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
this->m_container->log("Potree API listening on port "+QString::number(port)+"\n");
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &APIWebsockets::onNewConnection);
}
}
APIWebsockets::~APIWebsockets()
{
m_pWebSocketServer->close();
//qDeleteAll(m_clients.begin(), m_clients.end());
m_client = Q_NULLPTR;
}
void APIWebsockets::onNewConnection()
{
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
connect(pSocket, &QWebSocket::textMessageReceived, this, &APIWebsockets::processData);
connect(pSocket, &QWebSocket::disconnected, this, &APIWebsockets::socketDisconnected);
this->m_client = pSocket;
}
void APIWebsockets::processData(QString data)
{
this->m_container->log("\nData received:\n" + data);
}
void APIWebsockets::socketDisconnected()
{
m_client->deleteLater();
}
void APIWebsockets::initPointPicking()
{
this->m_container->log("init point picking");
this->m_client->sendTextMessage("{ \"type\":\"init_point_picking\", \"content\":{} }");
}