QT WebSocket ssl windows下 实现连接websockets

Qt WebSoket Ssl

#ifndef WEBSOCKETMANAGER_H
#define WEBSOCKETMANAGER_H

#include <QObject>
#include <QTimer>
#include <QWebSocket>
#include <QMutex>

#include "SslConfiguationTool.h"

/**
 * @brief webSocket 连接管理类
 */
class WebSocketManager : public QObject
{
    Q_OBJECT

public:
    /**
     * @brief 获取实例
     * @return
     */
    static WebSocketManager *getInstance();

    /**
     * @brief 连接服务器
     * @param ip
     * @param port
     */
    void connectSer(const QString &ip, const int port);

private slots:
    void onconnected();
    void closeConnection();
    void onTextMessageReceived(QString text);
    void reconnect();

private:
    explicit WebSocketManager(QObject *parent = nullptr);

     static WebSocketManager *instance;
     static QMutex mutex;

     QWebSocket m_websocket;

     int enableSsl = true;

};

#endif // WEBSOCKETMANAGER_H

#include "WebSocketManager.h"

WebSocketManager *WebSocketManager::instance = nullptr;
QMutex WebSocketManager::mutex;

WebSocketManager *WebSocketManager::getInstance()
{
    if (!instance) {
        QMutexLocker locker(&mutex);
        if (!instance) {
            instance = new WebSocketManager();
        }
    }
    return instance;
}

void WebSocketManager::connectSer(const QString &ip, const int port)
{
    qInfo() << Q_FUNC_INFO << ",ip:" << ip << ",port:" << port;

    QString wsPath("ws://");

    if (enableSsl) {
        wsPath = "wss://";
    }
    wsPath.append(ip).append(":").append(QString::number(port, 10));
    // 连接websocket
    m_websocket.open(QUrl(wsPath));
}

void WebSocketManager::onconnected()
{
    qInfo() << Q_FUNC_INFO;
}

void WebSocketManager::closeConnection()
{
    qInfo() << Q_FUNC_INFO;
    m_websocket.close();
}

void WebSocketManager::onTextMessageReceived(QString text)
{
    qInfo() << Q_FUNC_INFO << ",text:" << text;
}

void WebSocketManager::reconnect()
{
    qInfo() << Q_FUNC_INFO;
}

WebSocketManager::WebSocketManager(QObject *parent) : QObject(parent)
{
    connect(&m_websocket, SIGNAL(connected()), this, SLOT(onconnected()));
    connect(&m_websocket, SIGNAL(disconnected()),this, SLOT(closeConnection()));
    connect(&m_websocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));

    SslConfiguationTool::setSslConfiguation(m_websocket);
}

#ifndef SSLCONFIGUATIONTOOL_H
#define SSLCONFIGUATIONTOOL_H

#include <QWebSocket>

class SslConfiguationTool
{
public:
    static void setSslConfiguation(QWebSocket &webSocket);
};

#endif // SSLCONFIGUATIONTOOL_H

#include "SslConfiguationTool.h"

void SslConfiguationTool::setSslConfiguation(QWebSocket &webSocket)
{
    QSslConfiguration sslConf = webSocket.sslConfiguration();
    sslConf.setPeerVerifyMode(QSslSocket::VerifyNone);
    sslConf.setProtocol(QSsl::TlsV1SslV3);
    webSocket.setSslConfiguration(sslConf);
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值