How to check server life using QWebsocket ping pong.
-
Hi,
Do you mean you don't know how to start a QTimer when sending the ping ?
-
@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.
-
@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?
-
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.
-
@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 -
@mrjj
I dont know connectvoid 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? }); }
-
@bd9a said in How to check server life using QWebsocket ping pong.:
to what?
To a function from you where you restart the timer...
-
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 }); }
-
A short google for "lambda capture member variables" gave the following result:
In short, you need to capture
[this]
:QTimer::singleShot(5000, this, [this]() {
Regards
-