diff --git a/src/lib.rs b/src/lib.rs index ca1d5b6..58b5f21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -342,6 +342,21 @@ impl<'f, S> WebSocketWrite { { self.write_half.write_frame(&mut self.stream, frame).await } + + pub async fn flush(&mut self) -> Result<(), WebSocketError> + where + S: AsyncWrite + Unpin, + { + flush(&mut self.stream).await + } +} + +#[inline] +async fn flush(stream: &mut S) -> Result<(), WebSocketError> +where + S: AsyncWrite + Unpin, +{ + stream.flush().await.map_err(|e| WebSocketError::IoError(e)) } /// WebSocket protocol implementation over an async stream. @@ -495,6 +510,18 @@ impl<'f, S> WebSocket { Ok(()) } + /// Flushes the data from the underlying stream. + /// + /// if the underlying stream is buffered (i.e: TlsStream), it is needed to call flush + /// to be sure that the written frame are correctly pushed down to the bottom stream/channel. + /// + pub async fn flush(&mut self) -> Result<(), WebSocketError> + where + S: AsyncWrite + Unpin, + { + flush(&mut self.stream).await + } + /// Reads a frame from the stream. /// /// This method will unmask the frame payload. For fragmented frames, use `FragmentCollector::read_frame`.