Comments (8)
maybe my following comment is picky and/or incorrect, feel free to ignore.
In my case I was starting with a minimum test application that was opening the websocket and not writing/reading from it. My server was tearing down the connection because of missed pings, which I understood Tungstenite would automatically and immediately handle.
That didn't seem to be the case.
Then I added code to read from websocket and reply to pings from my test application. Then I noticed on the server I had two replies to pings, 1 presumably generated by Tungstenite and the other one by my application explicitly.
I removed the explicit replies and any socket writing in my application and I still saw ping replies.
Long story short, here's my understanding so far. Please let me know if this is incorrect.
- Does Tungstenite handle pings? If/when you read from the websocket, yes it will handle then and reply to them.
- Do I need to do any websocket write for the ping to be handled/answered? No. as long as you read from the websocket, Tungstenite will write the response for you regardless of you ever using the websocket for writing or not.
...oh, and by Tungstenite I may well be mixing Tungstenite and Tokio above.
Just wanted to share my observations in case they are of any help.
from tokio-tungstenite.
You need to read from the read-side of the socket so that it receives/handles pings, and on the next write it would then send the corresponding pong.
from tokio-tungstenite.
This is documented and belongs to Tokio. Tungstenite does nothing unless it is called. There are no things like background threads. If you don't call any of Tunstenite APIs, it does nothing. It just won't be there.
In WebSocket you could receive any messages at any time. Not only pings. Your application has to either poll WebSocket periodically or start a background job waiting for messages in order to handle them. If you do anything like that, Tungstenite will handle pings for you.
from tokio-tungstenite.
thanks!
This is likely a silly question but is it possible that pings are only replied to if my client reads anything from the socket?
My sample client was made of 2 peers, one sender-only (websocket 1) and one receiver-only (websocket 2).
On my sender-only code I was not doing any read from the websocket and I see on the server there are no ping/pongs for that client.
I updated my code to read and send pings when received, as I thought this crate was not replying to pings, and on the server then I saw 2 pongs, likely the one generated internally and the one created by me.
Does this make sense?
from tokio-tungstenite.
You need to read from the read-side of the socket so that it receives/handles pings
Is that documented? As IMHO it contradicts:
Even in async mode Tungstenite replies to pings automatically and immediately
from tokio-tungstenite.
Does Tungstenite handle pings? If/when you read from the websocket, yes it will handle then and reply to them.
Yes, it does. But the reading part should make some progress, so that the underlying functions which read from the socket (and write the response) do some progress.
Do I need to do any websocket write for the ping to be handled/answered? No. as long as you read from the websocket, Tungstenite will write the response for you regardless of you ever using the websocket for writing or not.
Right (but only for this particular case). In case you want to write some other messages explicitly, you have to make sure that the corresponding tasks make some progress. Normally this is ensured in "typical" server applications, i.e. when you split reading and writing part and there are tasks which use them.
from tokio-tungstenite.
@horacimacias it seems like the original question has been answered, so I'll close the issue (it just easier to track what is still open when the closed issues are closed ;), but in case there are any issues, feel free to re-open this one or open a new one.
from tokio-tungstenite.
Yes, pings are supported. Even in async mode Tungstenite replies to pings automatically and immediately. Ping behavior is covered by standard Autobahn tests. The client you're looking for is autobahn-client
from Tungstenite examples.
It is safe just to ignore pings as Tungstenite handles them for you.
from tokio-tungstenite.
Related Issues (20)
- Passing body in `connect`? HOT 2
- Can we provided a way to keep the original header name case. HOT 1
- 403 Forbidden HOT 1
- "WebSocket protocol error: httparse error: invalid token" when connect from ngrok tcp tunnel. HOT 5
- Is it possible to bind client to specific interface? HOT 1
- 没有连接断开时,直接检测客户端的状态的api,以实现重新连接,这在其它语言中都是常见的
- Performance boost implementing `poll_write_vectored` and rustls 0.23 HOT 2
- Is MaybeTlsStream supposed to work for servers too? HOT 2
- 0.20 and 0.21 break something HOT 25
- Vectorized send HOT 2
- new rustls 0.23.x is incompatible with the 0.22.x used by tokio-tungstenite HOT 2
- Adding authorization header leads in infinite block of connect_async HOT 2
- How to correctly handle large messages HOT 1
- How can I send pong frames as heartbeats from a client? HOT 4
- Specify default-features = false for rustls in Cargo.toml
- Websocket missing some ping messages. HOT 2
- tokio-tungstenite not using version 0.22 of tungstenite HOT 1
- no process-level CryptoProvider available -- call CryptoProvider::install_default() before this point HOT 5
- No option to enable `url` flag on `tungstenite` HOT 1
- How to reply to the client and send a message if an error occures on `accept_async` funtion. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-tungstenite.