Closed your-sudden-death closed 1 year ago
do you know if there is a way to open the connection in blocking mode using await, catch Errors in the Connection Process, and the handoff the connection to be held open by a async thread (e.g. tokio::spawn) while receiving messages on a callback function?
I don't understand what you're trying to achieve. tokio-tungstenite` is not "blocking", it's future-based. When you're trying to connect, you're getting a future back that you're supposed to poll. The future resolves once the connection is established, and then you could process the messages in a way you want (whether you need a callback or not).
The code that you posted is unfortunately overly complicated as the majority of its parts are not related to the issue you're describing and only a small section of it relates to tokio-tungstenite
. In that branch you create a select future and drop the result of this future right away (let _ = ...
), instead of awaiting on it. That's the reason why it behaves the way you described ("but this causes the connection to be dropped immediately").
In other words, if you want the select future to do some progress, you need to await
on it, instead of dropping it right away.
For more details of why this is important, please check the Tokio docs: https://tokio.rs/tokio/tutorial They have comprehensive tutorials on how to organise the code to properly work with futures.
Hi everyone, do you know if there is a way to open the connection in blocking mode using
await
, catch Errors in the Connection Process, and the handoff the connection to be held open by a async thread (e.g.tokio::spawn
) while receiving messages on a callback function? My first try was doing it like this:but this causes the connection to be dropped immediately, and the
.then
block is never executed If I do it how it is described in the client example, the call needs to await theselect
and block until the connection is closed, which is suboptimal.Thanks in advance