Struct tungstenite::protocol::WebSocketContext
source · 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.
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.
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.
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_message<Stream>(&mut self, stream: &mut Stream) -> Result<Message>where
Stream: Read + Write,
pub fn read_message<Stream>(&mut self, stream: &mut Stream) -> Result<Message>where Stream: Read + Write,
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_message<Stream>(
&mut self,
stream: &mut Stream,
message: Message
) -> Result<()>where
Stream: Read + Write,
pub fn write_message<Stream>( &mut self, stream: &mut Stream, message: Message ) -> Result<()>where Stream: Read + Write,
Send a message to the provided stream, if possible.
WebSocket will buffer a configurable number of messages at a time, except to reply to Ping
and Close requests. If the WebSocket’s send queue is full, SendQueueFull
will be returned
along with the passed message. Otherwise, the message is queued and Ok(()) is returned.
Note that only the last pong frame is stored to be sent, and only the most recent pong frame is sent if multiple pong frames are queued.
sourcepub fn write_pending<Stream>(&mut self, stream: &mut Stream) -> Result<()>where
Stream: Read + Write,
pub fn write_pending<Stream>(&mut self, stream: &mut Stream) -> Result<()>where Stream: Read + Write,
Flush the pending send queue.
sourcepub fn close<Stream>(
&mut self,
stream: &mut Stream,
code: Option<CloseFrame<'_>>
) -> Result<()>where
Stream: Read + Write,
pub fn close<Stream>( &mut self, stream: &mut Stream, code: Option<CloseFrame<'_>> ) -> Result<()>where Stream: Read + Write,
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 write(Message::Close(..))
.