80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
|
|
|
|
#pragma once
|
|
#include "logger.h"
|
|
#include "ltswebsocketserver_export.h"
|
|
#include "reply_message.h"
|
|
|
|
#include <QtCore/QObject>
|
|
#include <QtCore/QQueue>
|
|
#include <QtCore/QThread>
|
|
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
class QWebSocket;
|
|
#else
|
|
namespace LtsWebSocket { class QWebSocketStub; }
|
|
using QWebSocket = LtsWebSocket::QWebSocketStub;
|
|
namespace LtsWebSocket
|
|
{
|
|
class QWebSocketStub
|
|
{
|
|
public:
|
|
QWebSocketStub(QObject* parent = nullptr) { Q_UNUSED(parent); }
|
|
virtual ~QWebSocketStub() = default;
|
|
void sendTextMessage(const QString& /*message*/) {}
|
|
void close() {}
|
|
bool isValid() const { return false; }
|
|
QString errorString() const { return QString(); }
|
|
};
|
|
} // namespace LtsWebSocket
|
|
#endif
|
|
|
|
namespace LtsWebSocket
|
|
{
|
|
|
|
class LTSWEBSOCKETSERVER_EXPORT ClientConnection : public QThread, public ReplyHandler
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit ClientConnection(QWebSocket* socket, QObject* parent = nullptr);
|
|
|
|
~ClientConnection();
|
|
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
bool init();
|
|
|
|
void addReply(ReplyMessage* message) override;
|
|
|
|
QWebSocket* webSocket()
|
|
{
|
|
return m_socket;
|
|
}
|
|
#else
|
|
bool init() { return false; }
|
|
void addReply(ReplyMessage* /*message*/) override {}
|
|
QWebSocket* webSocket() { return nullptr; }
|
|
#endif
|
|
|
|
signals:
|
|
|
|
void newTextMessage(ClientConnection* client, const QString& message);
|
|
|
|
void disconnected(ClientConnection* client);
|
|
|
|
private slots:
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
void onNewTextMessageFromSocket(const QString& message);
|
|
void sendMessagesFromQueue();
|
|
void onSocketDisconnect();
|
|
#endif
|
|
|
|
private:
|
|
NGL::INGLLogger& m_logger;
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
void sendMessages();
|
|
#endif
|
|
QWebSocket* m_socket;
|
|
QQueue<ReplyMessage*> m_messageQueue;
|
|
};
|
|
} // namespace LtsWebSocket
|