I'm working on a Qt application which communicate with my web browser (currently Google Chrome) through a WebSocket.
Everything was working fine until I tried to use secured websockets...
I managed to resolve the issues I had with OpenSSL, so now I should have a working Qt application, but I don't.
I'm using VS2013, and Qt 5.3. I have the following code to start my server:
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);
}
I created the certificate files using the following method: https://developer.salesforce.com/blogs/developer-relations/2011/05/generating-valid-self-signed-certificates.html
On the browser side, I only have:
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!');
};
Unfortunately, everytime I tried, I got the following JS message:
WebSocket connection to 'wss://localhost:52132/' failed: Error in connection establishment: net::ERR_TIMED_OUT
So far:
QSslSocket::supportsSsl ()
returnstrue
- I don't have any
QSslSocket: cannot resolve XXX method
messages OpenSSL DLLs are loaded, VS2013 output the following messages:
'MyProject.exe' (Win32): Loaded 'I:\Sources\VS2013\x64\Release\ssleay32.dll'. Cannot find or open the PDB file. 'PressAndListenQt.exe' (Win32): Loaded 'I:\Sources\VS2013\x64\Release\libeay32.dll'. Cannot find or open the PDB file.`
I don't know how to find what's wrong, so I'm open to any suggestion!
Edit: I tried to generate a self-signed certificate using the following (instead of the link above):
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 100 -nodes
Now I get another error:
WebSocket connection to 'wss://localhost:52132/' failed: WebSocket opening handshake was canceled