snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

use sender and receiver very quick at same times #211

Closed playmyswift closed 2 years ago

playmyswift commented 2 years ago
let (ws_stream, _) = connect_async(case_url).await?;
let (mut sender, mut receiver) = ws_stream.split();
tokio::spawn(async move {
    loop {
        if let Ok(message) = receiver.try_next().await {
            // handle received message
        }
    }
});

tokio::spawn(async move {
    loop {
        // collect some message, then send it
        if let Ok(message) = trans_receiver.try_recv() { 
            sender.send(Message::Text(message)).await
        }
    }
});

can i use it very quick receive and send at same time ? may be 10 times in 1ms,

In my case, it worked normally for about 20s, then i can't receive message any more; i don't understand why it happend

daniel-abramov commented 2 years ago

can i use it very quick receive and send at same time ? may be 10 times in 1ms,

Yes, in theory. In practice, it depends on many factors and I would say tungstenite is definitely not a culprit here.

In my case, it worked normally for about 20s, then i can't receive message any more; i don't understand why it happend

We can't provide any help without a SSCCE or information. Based on your code it's hard to say what caused this behavior, but it's definitely not inside tokio-tungstenite I would say. I assume it's the issue with your usage of sender/receiver and internal buffering of those messages. We've already seen similar issues from other people, but in all cases it was related to the usage of channels, not with tokio-tungstenite. The described behavior sounds like a bug in the user code. Feel free to check other closed issues tagged as question, some people had similar problems.

I'm going to close this issue as there is no information to us to investigate/help. Feel free to re-open if you find something concrete where we could elaborate a bit.