简体   繁体   中英

How to get the "pong" response on "ping" call using Python websockets library?

I have an application that subscribes to a web-socket and, from time to time, receives from it some information. In some cases it might happen that my application does not receive anything for a relatively long time. In that case I might suspect that web-socket have "died". However, it might also be the case that web-socket just does not have anything to send and I just need to wait longer.

So, in such situations I do not want to guess what was the reason for the delay. I just want "actively" ask the web-socket if it is still alive. I have learned that one can do it with "ping" but I am not sure how exactly it works.

For example I can do the follwing:

pong_waiter = await ws.ping()

But what I should do with this object? I guess I need to see a "pong" string when the web-socket is alive, but where can I see it? What does return this string?

ADDED

If I execute:

pong_waiter = await ws.ping()
await pong_waiter

I did not get anything. After that I also execute:

pong_waiter.result()

It also returns nothing. But I know what web-socket is alive (I am still getting information from it).

ADDED 2

If I execute:

pong_waiter

I get:

<Future finished result=None>

So, where is "pong"?

You should await it if you want to wait for the pong :

pong_waiter = await ws.ping()
await pong_waiter

If you expect the pong to contain a custom payload, you can most likely get it from the pong_waiter Future by calling result() after it completes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM