Hol*_*olt 14 c++ ssl qt websocket qtwebsockets
我正在开发一个Qt应用程序,它通过WebSocket与我的网络浏览器(目前是谷歌浏览器)进行通信.
一切都工作正常,直到我尝试使用安全的websockets ...
我设法解决了我在OpenSSL中遇到的问题,所以现在我应该有一个有效的Qt应用程序,但我没有.
我正在使用VS2013和Qt 5.3.我有以下代码来启动我的服务器:
MyClass::MyClass (quint16 port, QObject *parent) :
QWebSocketServer("Press And Listen Server", QWebSocketServer::SecureMode, parent) {
QSslConfiguration sslConfiguration;
QFile certFile (QStringLiteral ("localhost.crt"));
QFile keyFile (QStringLiteral ("localhost.key"));
certFile.open (QIODevice::ReadOnly);
keyFile.open (QIODevice::ReadOnly);
QSslCertificate certificate (&certFile, QSsl::Pem);
QSslKey sslKey (&keyFile, QSsl::Rsa, QSsl::Pem);
certFile.close ();
keyFile.close ();
sslConfiguration.setPeerVerifyMode (QSslSocket::VerifyNone);
sslConfiguration.setLocalCertificate (certificate);
sslConfiguration.setPrivateKey (sslKey);
sslConfiguration.setProtocol (QSsl::TlsV1SslV3);
this->setSslConfiguration (sslConfiguration);
if (!this->listen (QHostAddress::Any, port)) {
throw ServerNotStartedException () ;
}
qDebug () << "Server listenning on: " << port ;
connect (this, &QWebSocketServer::newConnection, this, &MyClass::onNewConnection);
connect (this, &QWebSocketServer::closed, this, &MyClass::onClose);
connect (this, &QWebSocketServer::sslErrors, this, &MyClass::onSslErrors);
}
Run Code Online (Sandbox Code Playgroud)
我使用以下方法创建了证书文件:https://developer.salesforce.com/blogs/developer-relations/2011/05/generating-valid-self-signed-certificates.html
在浏览器方面,我只有:
var websock = websock = new WebSocket('wss://localhost:52132');
websock.onerror = function (error) {
console.log('Press & Listen, WS Error: ' + error);
};
websock.onopen = function () {
console.log('Open!');
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,每次我尝试,我都得到以下JS消息:
WebSocket connection to 'wss://localhost:52132/' failed: Error in connection establishment: net::ERR_TIMED_OUT
Run Code Online (Sandbox Code Playgroud)
至今:
QSslSocket::supportsSsl ()
回报 true
QSslSocket: cannot resolve XXX method
消息加载OpenSSL DLL,VS2013输出以下消息:
'MyProject.exe'(Win32):加载'I:\ Sources\VS2013\x64\Release\ssleay32.dll'.无法找到或打开PDB文件.'PressAndListenQt.exe'(Win32):加载'I:\ Sources\VS2013\x64\Release\libeay32.dll'.无法找到或打开PDB文件
我不知道如何找到错误,所以我愿意接受任何建议!
编辑:我尝试使用以下内容生成自签名证书(而不是上面的链接):
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 100 -nodes
Run Code Online (Sandbox Code Playgroud)
现在我又得到了一个错误:
WebSocket connection to 'wss://localhost:52132/' failed: WebSocket opening handshake was canceled
Run Code Online (Sandbox Code Playgroud)
小智 2
几天前我似乎也遇到了同样的情况。我解决了将自签名证书信任到可以使用 MMC 控制台打开的证书管理单元的问题。将您的证书导入到当前用户的个人文件夹和本地计算机文件夹中,还将证书导入到受信任的发布者文件夹中。请记住在测试后删除证书。
您也可以对教程 Qt\Examples\Qt-5.5\websockets\sslechoserver\ 的 localhost.cert 证书进行同样的操作,并查看 websockets 网页如何与教程提供的 wss 连接。
当网页中的 websocket 无法连接并出现相同错误时,这对我有用。
(抱歉,我的英语水平很低:))
归档时间: |
|
查看次数: |
3217 次 |
最近记录: |