websockets-rs / rust-websocket

A WebSocket (RFC6455) library written in Rust
http://websockets-rs.github.io/rust-websocket/
MIT License
1.55k stars 223 forks source link

send vs send_all #149

Closed dbrgn closed 7 years ago

dbrgn commented 7 years ago

I have a problem that probably results from a misunderstanding of the Tokio stack and/or your library, so I apologize in advance :)

I'm trying to implement the SaltyRTC client protocol using rust-websocket. The handshake messages are handled inside a future::loop_fn loop.

If you look at this file, the current code which uses send_all to send messages terminates the stream after having sent all messages.

boxed!(stream
    .send_all(outbox)
    .map(|(sink, _)| Loop::Continue(sink))
    .map_err(move |e| format!("Could not send message: {}", e).into()))

In contrast, when using the commented out code below instead, it properly blocks after having sent the message and waits for more messages from the server.

let outbox = stream::iter_ok::<_, WebSocketError>(messages);
boxed!(stream
    .send(messages[0].clone())
    .map(|sink| Loop::Continue(sink))
    .map_err(move |e| format!("Could not send message: {}", e).into()))

Am I doing something wrong? Shouldn't send and send_all behave similarly?

dbrgn commented 7 years ago

Resolved, see https://www.reddit.com/r/rust/comments/7e7pbo/stuck_with_tokio_send_vs_send_all/.

In contrast to send, send_all closes the sink after completing.