77 lines
1.7 KiB
C++
77 lines
1.7 KiB
C++
|
|
|
|
#pragma once
|
|
#include "logger.h"
|
|
|
|
#include <QtCore/QObject>
|
|
#include <QtCore/QSet>
|
|
#include <QtNetwork/QHostAddress>
|
|
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
#include <QtWebSockets/QWebSocketServer>
|
|
#include <QtWebSockets/qwebsocketprotocol.h>
|
|
#else
|
|
namespace LtsWebSocket
|
|
{
|
|
class QWebSocketServerStub;
|
|
}
|
|
using QWebSocketServer = LtsWebSocket::QWebSocketServerStub;
|
|
namespace QWebSocketProtocol
|
|
{
|
|
enum class CloseCode
|
|
{
|
|
NormalClosure = 1000
|
|
};
|
|
}
|
|
#endif
|
|
|
|
namespace LtsWebSocket
|
|
{
|
|
class MessageHandler;
|
|
class ClientConnection;
|
|
class ReplyMessage;
|
|
|
|
class ConnectionHandler : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit ConnectionHandler(QObject *parent = nullptr);
|
|
~ConnectionHandler();
|
|
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
bool init(const QString &serviceName, unsigned short port);
|
|
|
|
void sendToAllClients(ReplyMessage *message);
|
|
|
|
unsigned short webSocketPort() { return m_server->serverPort(); }
|
|
#else
|
|
bool init(const QString & /*serviceName*/, unsigned short /*port*/) { return false; }
|
|
void sendToAllClients(ReplyMessage * /*message*/) {}
|
|
unsigned short webSocketPort() { return 0; }
|
|
#endif
|
|
|
|
signals:
|
|
|
|
void newTextMessageFromClient(ClientConnection *client, const QString &message);
|
|
|
|
void clientDisconnected(ClientConnection *client);
|
|
|
|
void clientConnected(ClientConnection *client);
|
|
|
|
private slots:
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
void onServerError(QWebSocketProtocol::CloseCode closeCode);
|
|
void onNewConnection();
|
|
#endif
|
|
|
|
private:
|
|
NGL::INGLLogger &m_logger;
|
|
#ifdef QT6_WEBSOCKETS_AVAILABLE
|
|
QWebSocketServer *m_server;
|
|
#else
|
|
void *m_server;
|
|
#endif
|
|
QSet<ClientConnection *> m_connections;
|
|
};
|
|
} // namespace LtsWebSocket
|