Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWebSocket is not able to connect server
QtWS25 Last Chance

QWebSocket is not able to connect server

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.7k Views
  • 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.
  • SteMMoS Offline
    SteMMoS Offline
    SteMMo
    wrote on last edited by SteMMo
    #1

    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
    0
    • SteMMoS Offline
      SteMMoS Offline
      SteMMo
      wrote on last edited by
      #16

      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_1980A 1 Reply Last reply
      2
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #2

        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
        0
        • SteMMoS Offline
          SteMMoS Offline
          SteMMo
          wrote on last edited by
          #3

          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
          0
          • SteMMoS SteMMo

            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 Offline
            B Offline
            Bonnie
            wrote on last edited by
            #4

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

            SteMMoS 1 Reply Last reply
            1
            • B Bonnie

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

              SteMMoS Offline
              SteMMoS Offline
              SteMMo
              wrote on last edited by
              #5

              @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
              0
              • SteMMoS SteMMo

                @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 Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #6

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

                SteMMoS 2 Replies Last reply
                0
                • M Offline
                  M Offline
                  manordheim
                  wrote on last edited by
                  #7

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

                  SteMMoS 1 Reply Last reply
                  0
                  • M manordheim

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

                    SteMMoS Offline
                    SteMMoS Offline
                    SteMMo
                    wrote on last edited by
                    #8

                    @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
                    0
                    • B 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.

                      SteMMoS Offline
                      SteMMoS Offline
                      SteMMo
                      wrote on last edited by
                      #9

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

                      SteMMoS 1 Reply Last reply
                      0
                      • B 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.

                        SteMMoS Offline
                        SteMMoS Offline
                        SteMMo
                        wrote on last edited by
                        #10

                        @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
                        0
                        • SteMMoS SteMMo

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

                          SteMMoS Offline
                          SteMMoS Offline
                          SteMMo
                          wrote on last edited by SteMMo
                          #11

                          @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_1980A 1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by
                            #12

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

                            1 Reply Last reply
                            0
                            • SteMMoS 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_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              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.

                              SteMMoS 2 Replies Last reply
                              0
                              • aha_1980A aha_1980

                                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

                                SteMMoS Offline
                                SteMMoS Offline
                                SteMMo
                                wrote on last edited by
                                #14

                                @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
                                0
                                • aha_1980A aha_1980

                                  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

                                  SteMMoS Offline
                                  SteMMoS Offline
                                  SteMMo
                                  wrote on last edited by
                                  #15

                                  @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
                                  1
                                  • SteMMoS Offline
                                    SteMMoS Offline
                                    SteMMo
                                    wrote on last edited by
                                    #16

                                    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_1980A 1 Reply Last reply
                                    2
                                    • SteMMoS SteMMo

                                      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_1980A Offline
                                      aha_1980A Offline
                                      aha_1980
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #17

                                      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
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved