I created a simple chat application using QWebSocket and QWebSocketServer. The server side application is running on Windows Desktop. The client side application is running on the browser, and it was built with Qt for WebAssembly (Emscritpten version 3.1.56, Qt version 6.8). The wasm application is being served over a https connection, which only allows the usage of wss schema for the URL, as per Qt for WebAssembly's documentation:
"QWebSocket connections to any host. Note that web pages served overthe secure https protocol allows websockets connections over securewss protocol only."
.
My constructor on the Server side is as follows:
WebsocketServer::WebsocketServer(quint16 port, bool debug, QObject *parent) : QObject(parent),{ m_port = port; m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Websocket Chat Server"), QWebSocketServer::SecureMode, this); QSslConfiguration sslConfiguration; QFile certFile(QStringLiteral(":/certificates/localhost.cert")); QFile keyFile(QStringLiteral(":/certificates/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); m_pWebSocketServer->setSslConfiguration(sslConfiguration); QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &WebsocketServer::pollServerListenStatus); timer->start(1000);}
The slot for handling new connection is as follows:
void WebsocketServer::onNewConnection(){ QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); pSocket->ignoreSslErrors(); connect(pSocket, &QWebSocket::textMessageReceived, this, &WebsocketServer::processTextMessage); connect(pSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketServer::processBinaryMessage); connect(pSocket, &QWebSocket::disconnected, this, &WebsocketServer::socketDisconnected); connect(pSocket, &QWebSocket::errorOccurred, this, &WebsocketServer::onWebSocketErrorOccurred); m_clients << pSocket; Q_EMIT serverConnectedClients(m_clients.size());}
However, I cannot establish connection. I am getting the following messages on the client side:
"State changed: The socket has started establishing a connection. URL:"wss://mydomain.org:50000" Socket error: Socket error: The remote hostclosed the connection State changed: the socket is not connected".
What should I do to establish connection between client and server in this scenario?