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. How to check server life using QWebsocket ping pong.

How to check server life using QWebsocket ping pong.

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 6.6k Views 2 Watching
  • 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @bd9a said in How to check server life using QWebsocket ping pong.:

    how to do "if pong didnt received"?

    Can you explain a little bit what you mean here? From my pov when a pong doesn't arrive in a defined period of time I would close the connection.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    1
    • B Offline
      B Offline
      BD9a
      wrote on last edited by BD9a
      #3

      If server.php got closed, I got "disconnected" and trying to reconnect, so if the server get's up it will auto connect.
      But if I turned off server machine, there's no error or disconnect message, so I dont know when I can reconnect.

      Or just simply for check server availability, if didnt get "pong" server is "down".

      I dont know how to get "if pong didnt received".

      And for reconnection - is webSocket.open good?

      K 1 Reply Last reply
      0
      • B BD9a

        If server.php got closed, I got "disconnected" and trying to reconnect, so if the server get's up it will auto connect.
        But if I turned off server machine, there's no error or disconnect message, so I dont know when I can reconnect.

        Or just simply for check server availability, if didnt get "pong" server is "down".

        I dont know how to get "if pong didnt received".

        And for reconnection - is webSocket.open good?

        K Offline
        K Offline
        Kayote
        wrote on last edited by
        #4

        @bd9a
        "I dont know how to get "if pong didnt received"."
        As @Christian-Ehrlicher mentioned, in this case you could just use a timer which is started when you send the ping, and then if no response has been received within x seconds you know that the server is most likely down.

        B 3 Replies Last reply
        1
        • K Kayote

          @bd9a
          "I dont know how to get "if pong didnt received"."
          As @Christian-Ehrlicher mentioned, in this case you could just use a timer which is started when you send the ping, and then if no response has been received within x seconds you know that the server is most likely down.

          B Offline
          B Offline
          BD9a
          wrote on last edited by BD9a
          #5

          @kayote But how to get this pong? messagereceived signal and string message = "pong"?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @bd9a said in How to check server life using QWebsocket ping pong.:

            But how to get this pong?

            Maybe by taking a short look in the documentation of QWebSocket?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            B 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @bd9a said in How to check server life using QWebsocket ping pong.:

              But how to get this pong?

              Maybe by taking a short look in the documentation of QWebSocket?

              B Offline
              B Offline
              BD9a
              wrote on last edited by
              #7

              @christian-ehrlicher
              Idk what I mean saying this lol xd.

              I mean how my program can know "pong" didnt received in time.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @bd9a said in How to check server life using QWebsocket ping pong.:

                I mean how my program can know "pong" didnt received in time.

                By e.g. using a QTimer

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • B Offline
                  B Offline
                  BD9a
                  wrote on last edited by
                  #9

                  I know what I could use, but I have no idea how to do it :/

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Hi,

                    Do you mean you don't know how to start a QTimer when sending the ping ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • K Kayote

                      @bd9a
                      "I dont know how to get "if pong didnt received"."
                      As @Christian-Ehrlicher mentioned, in this case you could just use a timer which is started when you send the ping, and then if no response has been received within x seconds you know that the server is most likely down.

                      B Offline
                      B Offline
                      BD9a
                      wrote on last edited by
                      #11

                      @kayote said in How to check server life using QWebsocket ping pong.:

                      As @Christian-Ehrlicher mentioned, in this case you could just use a timer which is started when you send the ping, and then if no response has been received within x seconds you know that the server is most likely down.

                      I dont know how to do this.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @bd9a said in How to check server life using QWebsocket ping pong.:

                        I dont know how to do this.

                        We won't write you your program. What exactly do you don't know? How to restart the timer? Did you read the QTimer detail documentation?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Hi
                          well you hook up
                          void QWebSocket::pong(quint64 elapsedTime, const QByteArray &payload)
                          to a slot / lambda
                          then right after
                          you do
                          QWebSocket::ping(xxx)

                          then set up a timer

                          
                          bool TimeOutbeforePong=false; // this should be a member of the class  owning the QWebSocket
                          QTimer::singleShot(5000, this, [ & TimeOutbeforePong ](){      
                                TimeOutbeforePong = true;
                              ... do something...
                            });
                          

                          if you get your Pong while TimeOutbeforePong is false, server is alive
                          and you can ignore the timeout.

                          or similar code.

                          1 Reply Last reply
                          1
                          • K Kayote

                            @bd9a
                            "I dont know how to get "if pong didnt received"."
                            As @Christian-Ehrlicher mentioned, in this case you could just use a timer which is started when you send the ping, and then if no response has been received within x seconds you know that the server is most likely down.

                            B Offline
                            B Offline
                            BD9a
                            wrote on last edited by BD9a
                            #14

                            @kayote said in How to check server life using QWebsocket ping pong.:

                            and then if no response

                            How my program can know if no response, there is no bool "pongReceived" true or false.

                            @mrjj
                            Trying to implement it to my program. brb

                            mrjjM 1 Reply Last reply
                            0
                            • B BD9a

                              @kayote said in How to check server life using QWebsocket ping pong.:

                              and then if no response

                              How my program can know if no response, there is no bool "pongReceived" true or false.

                              @mrjj
                              Trying to implement it to my program. brb

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #15

                              @bd9a
                              well the timer will trigger after 5 seconds (or what you set it to)
                              and if you did not see the PONG signal in the meantime, then
                              server is not answering.

                              You might want to make the timer a member so you can cancel/stop it but
                              you can also just ignore the flag if you get your pong

                              B 1 Reply Last reply
                              1
                              • mrjjM mrjj

                                @bd9a
                                well the timer will trigger after 5 seconds (or what you set it to)
                                and if you did not see the PONG signal in the meantime, then
                                server is not answering.

                                You might want to make the timer a member so you can cancel/stop it but
                                you can also just ignore the flag if you get your pong

                                B Offline
                                B Offline
                                BD9a
                                wrote on last edited by
                                #16

                                @mrjj
                                I dont know connect

                                void QWebSocket::pong(quint64 elapsedTime, const QByteArray &payload)
                                

                                to what?

                                    timer = new QTimer(this);
                                    connect(timer, SIGNAL(timeout()),this,SLOT(trying()));
                                    timer->start(5000);
                                
                                void Socket::trying()
                                {
                                    bool TimeOutbeforePong=false; // this should be a member of the class  owning the QWebSocket
                                    QTimer::singleShot(5000, this, [ & TimeOutbeforePong ](){
                                          TimeOutbeforePong = true;
                                          webSocket.open(QUrl("xxx")); // Can it be here?
                                      });
                                }
                                
                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @bd9a said in How to check server life using QWebsocket ping pong.:

                                  to what?

                                  To a function from you where you restart the timer...

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  1
                                  • B Offline
                                    B Offline
                                    BD9a
                                    wrote on last edited by
                                    #18

                                    I need to access "TimeOutbeforePong" from other function, so I added it as private member, and I got these errors:

                                    private:
                                        bool TimeOutbeforePong=false;
                                    
                                    void Socket::trying()
                                    {
                                        TimeOutbeforePong=false;
                                        QTimer::singleShot(5000, this, [ & TimeOutbeforePong ](){ // TimeOutbeforePong in capture list does not name a variable
                                              TimeOutbeforePong = true; // this cannot be implicitly captured in this context
                                        });
                                    }
                                    
                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      BD9a
                                      wrote on last edited by
                                      #19

                                      Could sbody help me?

                                      1 Reply Last reply
                                      0
                                      • aha_1980A Offline
                                        aha_1980A Offline
                                        aha_1980
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        @bd9a

                                        A short google for "lambda capture member variables" gave the following result:

                                        https://stackoverflow.com/questions/7895879/using-member-variable-in-lambda-capture-list-inside-a-member-function

                                        In short, you need to capture [this]:

                                        QTimer::singleShot(5000, this, [this]() {

                                        Regards

                                        Qt has to stay free or it will die.

                                        1 Reply Last reply
                                        3
                                        • KutyusK Kutyus referenced this topic on

                                        • Login

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