Read all received messages from a Warp WebSocket

590 Views Asked by At

I am trying to create a game using WebSockets and am having difficulties understanding Streams. I know they are meant to be awaited asynchronously; however, I would like a different approach.

I have a game loop, which is spawned in its tokio task. At the beginning of every frame, the game loop should read all messages received from the WebSocket. I have yet to find out how I could do this without actively blocking the task until a message arrives. Moreover, I don't know how many messages there would be to read, as I want to read all messages received since the last frame.

I know I can read the messages from the WebSocket stream as shown below:

let (ws_sender, mut ws_rcv) = ws.split();
while let Some(result) = ws_rcv.next().await {
    // Handle result
}

My question is, if there is a way I could read the messages like this:

let (ws_sender, mut ws_rcv) = ws.split();
for result in ws_rcv.available_messages() {
    // Handle result
}

This approach would only read the values that have already been received and wouldn't block the thread.

0

There are 0 best solutions below