You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ifndef _H_SERVER_H_
#define _H_SERVER_H_
#include <QObject>
#include <QList>
#include <QByteArray>
#include <functional>
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)
/*
webSocketServer 只负责管理client的连接和收发数据
*/
typedef std::function<void(QWebSocket*, QByteArray)> RecvFunc;
class WebServer : public QObject
{
Q_OBJECT
public:
WebServer(QObject *parent = nullptr);
~WebServer();
bool openServer(int port, RecvFunc func);
bool closeServer();
void sendData(const QByteArray data);
int getCount() const;
signals:
void sgConnected(bool bFlag); //连接状态 bflag true 打开成功false 打开失败
void sgClientInfo(QString strIP, int port, bool bflag);
private:
Q_SLOT void onNewConnection();
Q_SLOT void processTextMessage(QString message);
Q_SLOT void processBinaryMessage(QByteArray message);
Q_SLOT void socketDisconnected();
Q_SLOT void closed();
private:
QWebSocketServer *m_pWebSocketServer{ nullptr };
QList<QWebSocket *> m_clients;
RecvFunc m_func;//数据接收回调函数
};
#endif