first commit
This commit is contained in:
+152
@@ -0,0 +1,152 @@
|
||||
#ifndef WEBSERVERCHAT_H
|
||||
#define WEBSERVERCHAT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QMap>
|
||||
#include <QFile>
|
||||
#include <QTimer>
|
||||
#include <QList>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QDateTime>
|
||||
|
||||
// Структура для стиля чата
|
||||
struct StyleChat {
|
||||
QString nick;
|
||||
QString context;
|
||||
QString blockColor;
|
||||
QString bColor; // Цвет фона страницы
|
||||
int fontSize;
|
||||
QString fontColor;
|
||||
QString borderColor;
|
||||
int borderSize;
|
||||
int padding;
|
||||
QString fontFamily;
|
||||
int timeMsg; // Время отображения в секундах
|
||||
bool freez;
|
||||
int transparency;
|
||||
|
||||
StyleChat() :
|
||||
fontSize(14),
|
||||
borderSize(2),
|
||||
padding(5),
|
||||
timeMsg(10),
|
||||
freez(false),
|
||||
transparency(255)
|
||||
{}
|
||||
};
|
||||
|
||||
// Структура для сообщения чата
|
||||
struct ChatMessage {
|
||||
QString nickname;
|
||||
QString content;
|
||||
qint64 timestamp;
|
||||
int timeMsg;
|
||||
bool freez;
|
||||
int transparency; // Исправлено с transporant на transparency
|
||||
|
||||
// Поля для стиля
|
||||
QString blockColor;
|
||||
QString borderColor;
|
||||
QString fontColor;
|
||||
QString fontFamily;
|
||||
int fontSize;
|
||||
int borderSize;
|
||||
int padding;
|
||||
|
||||
ChatMessage() :
|
||||
timestamp(0),
|
||||
timeMsg(10),
|
||||
freez(false),
|
||||
transparency(255),
|
||||
fontSize(14),
|
||||
borderSize(2),
|
||||
padding(5)
|
||||
{}
|
||||
};
|
||||
|
||||
class HttpServerChat : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HttpServerChat(const QStringList &fontList, int port, const QString &backgroundColor, QObject *parent = nullptr);
|
||||
~HttpServerChat();
|
||||
|
||||
bool start();
|
||||
void stop();
|
||||
|
||||
|
||||
void addMessage(const StyleChat &style);
|
||||
void activeServer(bool enabled);
|
||||
void setDeleteMode(bool deleteByTime, int maxMsgCount);
|
||||
void changeBackground(const QString &color);
|
||||
bool isFreez() const { return m_isFreez; }
|
||||
void setFreez(bool freez) { m_isFreez = freez; m_currentStyle.freez = freez; }
|
||||
int getMaxMsgCount() const { return m_maxMsgCount; }
|
||||
void setMaxMsgCount(int count) { m_maxMsgCount = count; }
|
||||
|
||||
|
||||
// block color
|
||||
QString getBlockColor() const;
|
||||
QString getBorderColor() const;
|
||||
QString getBackgroundColor() const;
|
||||
int getBorderSize() const;
|
||||
int getPadding() const;
|
||||
int getTransparency() const;
|
||||
// block font
|
||||
QString getFontFamily() const;
|
||||
int getFontSize() const;
|
||||
QString getFontColor() const;
|
||||
// block settings
|
||||
int getMessageTimeout() const;
|
||||
quint16 port() const;
|
||||
|
||||
|
||||
void setFontFamily(const QString &fontFamily);
|
||||
void setFontSize(int fontSize);
|
||||
void setFontColor(const QString &fontColor);
|
||||
void setBlockColor(const QString &blockColor);
|
||||
void setBorderColor(const QString &borderColor);
|
||||
void setBorderSize(int borderSize);
|
||||
void setPadding(int padding);
|
||||
void setMessageTimeout(int timeout);
|
||||
|
||||
void setTransparency(int transparency);
|
||||
|
||||
|
||||
signals:
|
||||
void serverStarted(bool success);
|
||||
|
||||
private slots:
|
||||
void onNewConnection();
|
||||
void readClient();
|
||||
void discardClient();
|
||||
|
||||
private:
|
||||
QTcpServer *m_server;
|
||||
QList<QTcpSocket*> m_clients;
|
||||
QList<ChatMessage> m_messages;
|
||||
|
||||
// Настройки
|
||||
QStringList m_fontList;
|
||||
StyleChat m_currentStyle;
|
||||
QString m_backgroundColor;
|
||||
bool m_deleteByTime; // true - удаление по времени, false - по количеству
|
||||
int m_maxMsgCount; // Максимальное количество сообщений
|
||||
QString colorNameToHex(const QString &colorName) const;
|
||||
void processRequest(QTcpSocket *socket, const QString &request);
|
||||
void sendResponse(QTcpSocket *socket, const QString &contentType, const QString &content, int statusCode = 200);
|
||||
void sendHtmlPage(QTcpSocket *socket);
|
||||
void sendJSONMessages(QTcpSocket *socket);
|
||||
void serveStaticFile(QTcpSocket *socket, const QString &filePath);
|
||||
QString getMimeType(const QString &filePath);
|
||||
QString generateHTML();
|
||||
QString generateJSON();
|
||||
void cleanupOldMessages();
|
||||
bool m_isFreez;
|
||||
};
|
||||
|
||||
#endif // WEBSERVERCHAT_H
|
||||
Reference in New Issue
Block a user