Qt WebSocket通讯封装(带测试用例)

  • 时间:
  • 来源:互联网
  • 文章标签:

Qt WebSocket通讯封装(带测试用例)

WebSocketClient

class WebSocketClientManager : public QObject
{
    Q_OBJECT
public:
    explicit WebSocketClientManager(QObject *parent = nullptr);
    ~WebSocketClientManager();
 
public:
    bool running() const;
 
    QString url() const;
    void setUrl(const QString &url);
 
signals:
    void signal_connected();
    void signal_disconnected();
    void signal_sendTextMessageResult(bool result);
    void signal_sendBinaryMessageResult(bool result);
    void signal_error(QString errorString);
    void signal_textFrameReceived(QString frame, bool isLastFrame);
    void signal_textMessageReceived(QString message);
    void signal_binaryDataReceived(QByteArray data);
 
public slots:
    void slot_start();
    void slot_stop();
    void slot_connectedTo(QString url);
    void slot_sendTextMessage(const QString &message);
    void slot_sendBinaryMessage(const QByteArray &data);
 
protected slots:
    void slot_connected();
    void slot_disconnected();
    void slot_error(QAbstractSocket::SocketError error);
    void slot_textFrameReceived(const QString &frame, bool isLastFrame);
    void slot_textMessageReceived(const QString &message);
    void slot_binaryDataReceived(const QByteArray &data);

private:
    bool _running;
    QString _url;
    bool _connected;
    QWebSocket *_pWebSocket;
};

WebSocketServer

class WebSocketServerManager : public QObject
{
    Q_OBJECT
public:
    explicit WebSocketServerManager(QString serverName,
                                    QWebSocketServer::SslMode secureMode = QWebSocketServer::NonSecureMode,
                                    QObject *parent = 0);
    ~WebSocketServerManager();
 
public:
    bool running() const;
 
signals:
    void signal_conncted(QString ip, qint32 port);
    void signal_disconncted(QString ip, qint32 port);
    void signal_sendTextMessageResult(QString ip, quint32 port, bool result);
    void signal_sendBinaryMessageResult(QString ip, quint32 port, bool result);
    void signal_error(QString ip, quint32 port, QString errorString);
    void signal_textFrameReceived(QString ip, quint32 port, QString frame, bool isLastFrame);
    void signal_textMessageReceived(QString ip, quint32 port,QString message);
    void signal_binaryDataReceived(QString ip, qint32 port, QByteArray data);
    void signal_close();
 
public slots:
    void slot_start(QHostAddress hostAddress = QHostAddress(QHostAddress::Any), qint32 port = 10080);
    void slot_stop();
    void slot_sendData(QString ip, qint32 port, QString message);
    void slot_sendData(QString ip, qint32 port, QByteArray data);
    void slot_sendData(QString message);
    void slot_sendData(QByteArray data);

protected slots:
    void slot_newConnection();
    void slot_serverError(QWebSocketProtocol::CloseCode closeCode);
    void slot_closed();
 
protected slots:
    void slot_disconnected();
    void slot_error(QAbstractSocket::SocketError error);
    void slot_textFrameReceived(const QString &frame, bool isLastFrame);
    void slot_textMessageReceived(const QString &message);
    void slot_binaryReceived(const QByteArray &data);

private:
    QString _serverName;
    QWebSocketServer::SslMode _sslMode;
    bool _running;
    QWebSocketServer *_pWebSocketServer;
    QHash<QString, QWebSocket*> _hashIpPort2PWebSocket;
    QHostAddress _listenHostAddress;
    qint32 _listenPort;
};
Qt基于QWebSocketServer,WebSocket的封装,和TCP/UDP一样,简单易用,直接引用头文件与资源文件,然后绑定几个信号与槽即可,并且跨平台测试通过。

测试用例:
在这里插入图片描述
源码地址下载:

https://gitee.com/zlozl5566/qt-learning-notes/tree/master/MyWebSocket

本文链接http://www.taodudu.cc/news/show-1781814.html