1、创建QWebSocketServer的设置SslMode配置为SecureMode
webserver = new QWebSocketServer("webserver", QWebSocketServer::SecureMode, this);
2、配置SSL
QSslConfiguration conf = webserver->sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setLocalCertificate(cert);
conf.setPrivateKey(key);
conf.setProtocol(QSsl::TlsV1SslV3);
webserver->setSslConfiguration(conf);
3、关联信号槽
connect(webserver, SIGNAL(newConnection()), this, SLOT(newConnection()));
connect(webserver, SIGNAL(closed()), webserver, SLOT(deleteLater()));
完整构造函数
webclient = Q_NULLPTR;
webserver = new QWebSocketServer("webserver", QWebSocketServer::SecureMode, this);
QFile certFile(":/localhost.cert");
QFile keyFile(":/localhost.key");
if(!certFile.open(QIODevice::ReadOnly)){
qDebug() << "certfile open failed...";
}
if(!keyFile.open(QIODevice::ReadOnly)){
qDebug() << "keyfile open failed...";
}
QSslCertificate cert(&certFile, QSsl::Pem);
QSslKey key(&keyFile, QSsl::Rsa, QSsl::Pem);
certFile.close();
keyFile.close();
QSslConfiguration conf = webserver->sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setLocalCertificate(cert);
conf.setPrivateKey(key);
conf.setProtocol(QSsl::TlsV1SslV3);
webserver->setSslConfiguration(conf);
connect(webserver, SIGNAL(newConnection()), this, SLOT(newConnection()));
connect(webserver, SIGNAL(closed()), webserver, SLOT(deleteLater()));
源代码下载:https://download.csdn.net/download/cqchengdan/11439286