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
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.
@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
@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 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?
Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog
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
@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