0

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 over the secure https protocol allows websockets connections over secure wss 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 host closed the connection State changed: the socket is not connected".

What should I do to establish connection between client and server in this scenario?

2
  • 1
    just a thought - have you tried non-secure websockets? If those work, your problem would seem to be somewhere with the security. Commented Dec 23, 2024 at 19:03
  • Non secure WebSockets work normally. The problem is that if your wasm app is served on a https connection, connections using ws are not allowed. You have to use wss instead. However, secure websockets and openssl are not supported in Qt for Webassembly. Commented Dec 27, 2024 at 14:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.