pub struct WebSocketContext { /* private fields */ }Expand description
A context for managing WebSocket stream.
Implementations§
Source§impl WebSocketContext
impl WebSocketContext
Sourcepub fn new(mode: OperationMode, config: Option<WebSocketConfig>) -> Self
pub fn new(mode: OperationMode, 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>,
mode: OperationMode,
config: Option<WebSocketConfig>,
) -> Self
pub fn from_partially_read( part: Vec<u8>, mode: OperationMode, 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, func: impl FnOnce(&mut WebSocketConfig))
pub fn set_config(&mut self, 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<T: Read + Write>(&mut self, stream: &mut T) -> Result<Message>
pub fn read<T: Read + Write>(&mut self, stream: &mut T) -> 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<T: Read + Write>(
&mut self,
stream: &mut T,
msg: Message,
) -> Result<()>
pub fn write<T: Read + Write>( &mut self, stream: &mut T, msg: 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.