Merge pull request #105 from snapview/can-read-write

websocket: add `can_read()` and `can_write()`
pull/106/head
Daniel Abramov 5 years ago committed by GitHub
commit 61f46be644
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      src/protocol/mod.rs

@ -110,6 +110,21 @@ impl<Stream> WebSocket<Stream> {
pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) {
self.context.set_config(set_func)
}
/// Check if it is possible to read messages.
///
/// Reading is impossible after receiving `Message::Close`. It is still possible after
/// sending close frame since the peer still may send some data before confirming close.
pub fn can_read(&self) -> bool {
self.context.can_read()
}
/// Check if it is possible to write messages.
///
/// Writing gets impossible immediately after sending or receiving `Message::Close`.
pub fn can_write(&self) -> bool {
self.context.can_write()
}
}
impl<Stream: Read + Write> WebSocket<Stream> {
@ -239,6 +254,21 @@ impl WebSocketContext {
set_func(&mut self.config)
}
/// Check if it is possible to read messages.
///
/// Reading is impossible after receiving `Message::Close`. It is still possible after
/// sending close frame since the peer still may send some data before confirming close.
pub fn can_read(&self) -> bool {
self.state.can_read()
}
/// Check if it is possible to write messages.
///
/// Writing gets impossible immediately after sending or receiving `Message::Close`.
pub fn can_write(&self) -> bool {
self.state.is_active()
}
/// Read a message from the provided stream, if possible.
///
/// This function sends pong and close responses automatically.

Loading…
Cancel
Save