Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebSocket: unable to connect to HTTPS server



  • Hi,

    I've the need to connect to a web-server using QWebSocket for a chat application. Following the tutorials (Tutorials), I have created a class who opens the connection to that server, but when listening to errors, I always get QAbstractSocket::ConnectionRefusedError. Finally, QWebSocket::disconnected is called. Connected event is never called:

    The source code is:
    -> MY_SOCKET.H

    #pragma once
    #include "QtWebSockets/qwebsocket.h"
    
    class MySocket : public  QObject
    {
        Q_OBJECT
    
    public:
        explicit MySocket(QObject *parent = nullptr);
    
    private Q_SLOTS:
        void onConnected();
        void onDisconnected();
    
    private:
    
        QWebSocket m_webSocket;
    };
    

    -> MY_SOCKET.CPP

    #include "my_socket.h"
    #include <qDebug>
    
    MySocket::MySocket(QObject *parent) : QObject(parent), m_webSocket("eff")
    {
        QObject::connect(&m_webSocket, &QWebSocket::connected, this, &MySocket::onConnected);
        QObject::connect(&m_webSocket, &QWebSocket::disconnected, this, &MySocket::onDisconnected);
        QObject::connect(&m_webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
            [=](QAbstractSocket::SocketError error) {
            qDebug() << "[SocketClient] Error: " << error;
        }
        );
    
        QUrl server("wss://myServer/");
    
        qDebug() << "[SocketClient] Do connection to: " << server;
        qDebug() << "[SocketClient] Scheme: " << server.scheme();
        qDebug() << "[SocketClient] Host " << server.host();
        qDebug() << "[SocketClient] Port " << server.port();
        qDebug() << "[SocketClient] Is valid " << server.isValid();
    
        
        qDebug() << "[SocketClient] state:" << m_webSocket.state();
        m_webSocket.open(server);
        qDebug() << "[SocketClient] state:" << m_webSocket.state();
    }
    
    void MySocket::onConnected()
    {
        qDebug() << "[SocketClient] WebSocket connected";
    }
    
    void MySocket::onDisconnected()
    {
        qDebug() << "[SocketClient] WebSocket disconnected";
    }
    

    This is what I get when running it:

    [SocketClient] Do connection to:  QUrl("wss://myServer/")
    [SocketClient] Scheme:  "wss"
    [SocketClient] Host  "myServer"
    [SocketClient] Port  -1
    [SocketClient] Is valid  true
    [SocketClient] state: QAbstractSocket::UnconnectedState
    [SocketClient] state: QAbstractSocket::ConnectingState
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    [SocketClient] Error:  QAbstractSocket::ConnectionRefusedError
    ...
    [SocketClient] Error:  QAbstractSocket::RemoteHostClosedError
    [SocketClient] Error:  QAbstractSocket::RemoteHostClosedError
    [SocketClient] WebSocket disconnected
    

    What I'm doing wrong? Any help will be appreciated.



  • @Adso4
    For https, what have you done about installing SSL libraries? You need them.


Log in to reply