添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
风流的墨镜  ·  2025 IEEE Life ...·  1 周前    · 
千杯不醉的瀑布  ·  IEEE Honors Thrasos ...·  1 周前    · 
爱喝酒的香槟  ·  怎样利用digital ...·  1 月前    · 
乖乖的围巾  ·  xarray.open_mfdataset·  3 月前    · 
阳刚的消防车  ·  湖南省脑卒中30 ...·  7 月前    · 
Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Hello, how can I check server life using qwebsocket ping, how to do "if pong didnt received"?

Websocket server in php, Client in qt.

@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

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?

@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

@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

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

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