Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flush method to ensure frame is correctly written #88

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ impl<'f, S> WebSocketWrite<S> {
{
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<S>(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.
Expand Down Expand Up @@ -495,6 +510,18 @@ impl<'f, S> WebSocket<S> {
Ok(())
}

/// Flushes the data from the underlying stream.
///
/// if the underlying stream is buffered (i.e: TlsStream<TcpStream>), 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`.
Expand Down
Loading