pub struct WebSocketContext { /* private fields */ }
Expand description
A context for managing WebSocket stream.
Implementations§
Source§impl WebSocketContext
impl WebSocketContext
Sourcepub fn new(role: Role, config: Option<WebSocketConfig>) -> Self
pub fn new(role: Role, config: Option<WebSocketConfig>) -> Self
Create a WebSocket context that manages a post-handshake stream.
§Panics
Panics if config is invalid e.g. max_write_buffer_size <= write_buffer_size
.
Sourcepub fn from_partially_read(
part: Vec<u8>,
role: Role,
config: Option<WebSocketConfig>,
) -> Self
pub fn from_partially_read( part: Vec<u8>, role: Role, config: Option<WebSocketConfig>, ) -> Self
Create a WebSocket context that manages an post-handshake stream.
§Panics
Panics if config is invalid e.g. max_write_buffer_size <= write_buffer_size
.
Sourcepub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig))
pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig))
Change the configuration.
§Panics
Panics if config is invalid e.g. max_write_buffer_size <= write_buffer_size
.
Sourcepub fn get_config(&self) -> &WebSocketConfig
pub fn get_config(&self) -> &WebSocketConfig
Read the configuration.
Sourcepub fn can_read(&self) -> bool
pub fn can_read(&self) -> bool
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.
Sourcepub fn can_write(&self) -> bool
pub fn can_write(&self) -> bool
Check if it is possible to write messages.
Writing gets impossible immediately after sending or receiving Message::Close
.
Sourcepub fn read<Stream>(&mut self, stream: &mut Stream) -> Result<Message>
pub fn read<Stream>(&mut self, stream: &mut Stream) -> Result<Message>
Read a message from the provided stream, if possible.
This function sends pong and close responses automatically. However, it never blocks on write.
Sourcepub fn write<Stream>(
&mut self,
stream: &mut Stream,
message: Message,
) -> Result<()>
pub fn write<Stream>( &mut self, stream: &mut Stream, message: Message, ) -> Result<()>
Write a message to the provided stream.
A subsequent call should be made to flush
to flush writes.
In the event of stream write failure the message frame will be stored
in the write buffer and will try again on the next call to write
or flush
.
If the write buffer would exceed the configured WebSocketConfig::max_write_buffer_size
Err(WriteBufferFull(msg_frame))
is returned.
Sourcepub fn flush<Stream>(&mut self, stream: &mut Stream) -> Result<()>
pub fn flush<Stream>(&mut self, stream: &mut Stream) -> Result<()>
Flush writes.
Ensures all messages previously passed to write
and automatically
queued pong responses are written & flushed into the stream
.
Sourcepub fn close<Stream>(
&mut self,
stream: &mut Stream,
code: Option<CloseFrame<'_>>,
) -> Result<()>
pub fn close<Stream>( &mut self, stream: &mut Stream, code: Option<CloseFrame<'_>>, ) -> Result<()>
Close the connection.
This function guarantees that the close frame will be queued.
There is no need to call it again. Calling this function is
the same as calling send(Message::Close(..))
.