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

QWebSocket is not able to connect server



  • Hi all,
    I'm trying to develop a simple websocket client for a Linux machine (embedded board with Debian GNU/Linux 9.3 (stretch) and Qt 5.7.1).
    My code is the following (embedded in a class):

    ...
    QObject::connect( &_ws, &QWebSocket::connected, this, &IoBoard::onConnected);
    QObject::connect( &_ws, &QWebSocket::disconnected, this, &IoBoard::onDisconnected);
    
    connect( &_ws, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
        [=](QAbstractSocket::SocketError error){
            qDebug() << "[IoBoard] Error: " << error;
        }
    );
    
    QUrl url ("ws://localhost:7681");
    url.setHost("10.191.40.216");
    qDebug() << "[IoBoard] url: " << url.toString() << " - Scheme: " << url.scheme() << " - Host: " << url.host() << " - Port: " << url.port();
    qDebug() << "[IoBoard] state:" << _ws.state();
    _ws.open(url);
    qDebug() << "[IoBoard] state:" << _ws.state();
    ...
    

    When I run it I have:

    [IoBoard] url:  "ws://10.191.40.216:7681"  - Scheme:  "ws"  - Host:  "10.191.40.216"  - Port:  7681
    [IoBoard] state: QAbstractSocket::UnconnectedState
    [IoBoard] state: QAbstractSocket::ConnectingState
    

    Nothing more: 'connected' slot is never called and the server does not receive any connection.
    The server is running correctly cause if I run a webpage based on Javascipt websocket I have connection.
    Any clue?

    PS: I overrited the address just to test the remote connection: same result.

    Thanks, regards



  • Solved!

    Comparing main.cpp file I found that the definition of IoBoard object was static and global (outside the main() function).
    Now I defined the object inside the main() function and the connection is done.



  • Is _ws a member variable?
    Try to also connect and print the destroyed signal to see if it is destroyed somewhere.



  • Yes, simply:

    private:
        QWebSocket _ws;
    

    I've connected the destroyed signal but it was recalled at the exit of the program:

    [IoBoard] url:  "ws://10.191.40.216:7681"  - Scheme:  "ws"  - Host:  "10.191.40.216"  - Port:  7681
    [IoBoard] state: QAbstractSocket::UnconnectedState
    [IoBoard] state: QAbstractSocket::ConnectingState
    ...
    Platform:  "xcb"
    ...
    [IoBoard] Destroyed
    

    Thanks



  • @SteMMo
    Then it lives until exit. Looks fine.
    So there is no any error signal? Even no timeout? That's weird.
    Does stateChanged signal have more information?



  • @Bonnie
    Added a new slot, now the debug output is:

    [IoBoard] url:  "ws://localhost:7681"  - Scheme:  "ws"  - Host:  "localhost"  - Port:  7681
    [IoBoard] state: QAbstractSocket::UnconnectedState
    [IoBoard] StateChanged: QAbstractSocket::ConnectingState
    [IoBoard] state: QAbstractSocket::ConnectingState
    ...
    [IoBoard] Destroyed
    QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()


  • @SteMMo
    When you said "exit of the program", did you exit manually? Or the program just ends?
    If you exit manually, how about waiting for like 10 minutes before your exit?
    If your project is simple, it might also help if you post more code.



  • Do you have access to the server? Does the server have anything to say?



  • @manordheim as I said in the previous message, I'm able to connect the server (it is my program running on the same machine) by a web page and websocket library via Javascript.
    They communicate correctly.



  • @Bonnie I mean that I run the program, then I shut down the program by Alt-F4.
    I start a 10-minutes test ...



  • @Bonnie
    Right now the connection is hosted in the constructor of a global object.

    /**
     * @brief IoBoard::IoBoard
     */
    IoBoard::IoBoard(QObject* parent) : QObject(parent)
    {
        qDebug() << "Costruttore IoBoard";
    
        // -- websocket
    
        QObject::connect( &_ws, &QWebSocket::connected, this, &IoBoard::onConnected);
        QObject::connect( &_ws, &QWebSocket::disconnected, this, &IoBoard::onDisconnected);
        QObject::connect( &_ws, &QWebSocket::destroyed, this, &IoBoard::onDestroyed);
        QObject::connect( &_ws, &QWebSocket::stateChanged, this, &IoBoard::onStateChanged);
    
        connect( &_ws, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
            [=](QAbstractSocket::SocketError error){
                qDebug() << "[IoBoard] Errore: " << error;
            }
        );
    
        QUrl url ("ws://localhost:7681");
        // url.setHost("10.191.40.216");
        qDebug() << "[IoBoard] url: " << url.toString() << " - Scheme: " << url.scheme() << " - Host: " << url.host() << " - Port: " << url.port();
        qDebug() << "[IoBoard] state:" << _ws.state();
        _ws.open(url);
        qDebug() << "[IoBoard] state:" << _ws.state();
    }
    


  • @Bonnie
    More than half an hour and nothing is notified.
    I also press some buttons and return but websocket does not notify anything.

    Costruttore IoBoard
    [IoBoard] url:  "ws://localhost:7681"  - Scheme:  "ws"  - Host:  "localhost"  - Port:  7681
    [IoBoard] state: QAbstractSocket::UnconnectedState
    [IoBoard] StateChanged: QAbstractSocket::ConnectingState
    [IoBoard] state: QAbstractSocket::ConnectingState
    
    Costruttore Cassetti
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    [Cassetti] open:  true
    >> cassetti: 19
    Costruttore Prenotazioni
    Platform:  "xcb"
    Farmacia:  "Locker 1 - Gruppo AC - Roman"
    Cassetti:  "99"
    serial:  "ttyS1"
    [CheckCode] Tipo:  0  - Code: "99887766"
    qml: onActFocCh
    qml: OK - 123
    qml: OK - 123
    qml: onActFocCh
    qml: onActFocCh
    qml: onActFocCh
    qml: onActFocCh
    qml: onActFocCh
    qml: onActFocCh
    qml: onActFocCh
    


  • What's you Qt version?
    Can you provide a minimal code example for others to test?


  • Lifetime Qt Champion

    Hi @SteMMo,

    Have you already tried the Echo Client Example and checked if that can connect to your server?

    Btw, Qt 5.7.1 is quite old. Can you update to a newer version?

    Regards



  • @aha_1980 Hi,
    at the moment, no.
    The boards on the field mount an old OS (Debian stretch); this repository provides version 5.7.1
    Maybe in the future I'll be able to move on a new version (buster)



  • @aha_1980 I tried the sample and yes, it worked:

    root@tinkerboard:/home/amtek/echoClient# ./echoclient -d 
    WebSocket server: QUrl("ws://localhost:7681")
    WebSocket connected
    Message received: "Unk"
    

    I don't understand: my code for IoBoard is basically started from the code of EchoClient class .. the code is included in a past message ..



  • Solved!

    Comparing main.cpp file I found that the definition of IoBoard object was static and global (outside the main() function).
    Now I defined the object inside the main() function and the connection is done.


  • Lifetime Qt Champion

    Hi @SteMMo,

    I'm glad you figured it out. So please mark this topic as SOLVED too.

    Thanks!


Log in to reply