Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved QWebSocket is not able to connect server

    General and Desktop
    4
    17
    792
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SteMMo
      SteMMo last edited by SteMMo

      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

      1 Reply Last reply Reply Quote 0
      • SteMMo
        SteMMo last edited by

        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.

        aha_1980 1 Reply Last reply Reply Quote 2
        • B
          Bonnie last edited by Bonnie

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

          1 Reply Last reply Reply Quote 0
          • SteMMo
            SteMMo last edited by

            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

            B 1 Reply Last reply Reply Quote 0
            • B
              Bonnie @SteMMo last edited by

              @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?

              SteMMo 1 Reply Last reply Reply Quote 1
              • SteMMo
                SteMMo @Bonnie last edited by

                @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()
                
                B 1 Reply Last reply Reply Quote 0
                • B
                  Bonnie @SteMMo last edited by Bonnie

                  @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.

                  SteMMo 2 Replies Last reply Reply Quote 0
                  • M
                    manordheim last edited by

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

                    SteMMo 1 Reply Last reply Reply Quote 0
                    • SteMMo
                      SteMMo @manordheim last edited by

                      @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.

                      1 Reply Last reply Reply Quote 0
                      • SteMMo
                        SteMMo @Bonnie last edited by

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

                        SteMMo 1 Reply Last reply Reply Quote 0
                        • SteMMo
                          SteMMo @Bonnie last edited by

                          @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();
                          }
                          
                          1 Reply Last reply Reply Quote 0
                          • SteMMo
                            SteMMo @SteMMo last edited by SteMMo

                            @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
                            
                            aha_1980 1 Reply Last reply Reply Quote 0
                            • B
                              Bonnie last edited by

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

                              1 Reply Last reply Reply Quote 0
                              • aha_1980
                                aha_1980 Lifetime Qt Champion @SteMMo last edited by

                                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

                                Qt has to stay free or it will die.

                                SteMMo 2 Replies Last reply Reply Quote 0
                                • SteMMo
                                  SteMMo @aha_1980 last edited by

                                  @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)

                                  1 Reply Last reply Reply Quote 0
                                  • SteMMo
                                    SteMMo @aha_1980 last edited by

                                    @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 ..

                                    1 Reply Last reply Reply Quote 1
                                    • SteMMo
                                      SteMMo last edited by

                                      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.

                                      aha_1980 1 Reply Last reply Reply Quote 2
                                      • aha_1980
                                        aha_1980 Lifetime Qt Champion @SteMMo last edited by

                                        Hi @SteMMo,

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

                                        Thanks!

                                        Qt has to stay free or it will die.

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post