Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Services/ExerciseEventManagement/WebSocketServer/web_socket_handler.cpp
T
torstenandCopilot 7a6be7474e Qt6 migration: Fix BUILD_RTI=OFF build errors in TrainingAreaBase
- training_area_telegrams.cpp: Fixed QJsonObject initializer syntax (Qt6 requires {{}} not ({}))
- training_area_telegrams.cpp: Fixed fromJson() methods to access m_* members directly
- training_area_telegrams.cpp: Fixed brace mismatches in multiple toJson() methods
- training_area_telegrams.h: Changed Header struct to public members for consistency
- tx_mixin.cpp: Fixed getEquipmentNumber() template for direct member access
- lts_coordinate.cpp: Added missing #include <iomanip>
- MIGRATION_STATUS.md: Added migration progress tracking document

C++17 constraint enforced due to RTI Connext DDS 7.7.0 incompatibility with C++20+

Build status: BUILD_RTI=OFF successful (0 errors, warnings only)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-23 20:49:23 +02:00

115 lines
2.7 KiB
C++
Executable File

/**
* Websocket interface for building control unit
* $Id$
* $HeadURL$
* $Author$
* Created by Christian Ehrlicher, SLSE, 2017-01-30
*/
#include "web_socket_handler.h"
#include <utility>
#include <QtWebSockets/QWebSocket>
#include <iostream>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonValue>
#include <QtCore/QJsonDocument>
namespace ExerciseEventManagement
{
WebSocketHandler::WebSocketHandler(QObject *parent) : QObject(parent)
, m_webSocketServer(new QWebSocketServer(QStringLiteral("AVS-Server"), QWebSocketServer::NonSecureMode, this))
, m_timer(new QTimer()),
m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger()) {
connect(m_timer, &QTimer::timeout,
this, &WebSocketHandler::onSendMessage);
m_timer->start(1000);
}
WebSocketHandler::~WebSocketHandler()
{
for (auto s : std::as_const(m_webSockets))
{
s->disconnect(this);
s->close();
}
qDeleteAll(m_webSockets);
}
bool WebSocketHandler::init(quint16 listenPort)
{
if (!m_webSocketServer->listen(QHostAddress::Any, listenPort))
{
WARNING_LOG(m_logger) << "Can not listen on port " << listenPort << " - aborting";
return false;
}
// inform calling process via stdout about the port/url
std::cout << "web socket listen port: " << m_webSocketServer->serverPort() << std::endl;
std::cout << "web socket url: " << m_webSocketServer->serverUrl().toString() << std::endl;
connect(m_webSocketServer, &QWebSocketServer::newConnection,
this, &WebSocketHandler::onNewConnection);
return true;
}
bool WebSocketHandler::sendTelegram(const QString &jsonStr)
{
if (!isConnected())
return false;
ERROR_LOG(m_logger) << "jsonStr" << jsonStr;
bool bOk = false;
for (QWebSocket *ws : std::as_const(m_webSockets))
{
if (ws->sendTextMessage(jsonStr))
bOk = true;
}
return bOk;
}
void WebSocketHandler::onNewConnection()
{
QWebSocket *ws = m_webSocketServer->nextPendingConnection();
if (!ws)
return;
INFO_LOG(m_logger) << "New websocket connection from " << ws->peerAddress().toString() << ":" << ws->peerPort();
connect(ws, &QWebSocket::disconnected,
this, &WebSocketHandler::onWebSocketDisconnected);
m_webSockets += ws;
emit webSocketConnected(ws);
}
void WebSocketHandler::onSendMessage()
{
ERROR_LOG(m_logger) << "Send message: ";
QJsonObject obj;
obj[QStringLiteral("eventTime")] = QJsonValue(QDateTime::currentMSecsSinceEpoch());
QString jsonStr = QString::fromUtf8(QJsonDocument(obj).toJson());
sendTelegram(jsonStr);
}
void WebSocketHandler::onWebSocketDisconnected()
{
QWebSocket *ws = qobject_cast<QWebSocket *>(sender());
if (!ws)
return;
m_webSockets.remove(ws);
ws->deleteLater();
}
} // namespace ExerciseEventManagement