Struct WebSocketContext

struct WebSocketContext { ... }

A context for managing WebSocket stream.

Implementations

impl WebSocketContext

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.

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.

fn set_config<impl FnOnce(&mut WebSocketConfig): FnOnce(&mut WebSocketConfig)>(self: &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.

fn get_config(self: &Self) -> &WebSocketConfig

Read the configuration.

fn can_read(self: &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.

fn can_write(self: &Self) -> bool

Check if it is possible to write messages.

Writing gets impossible immediately after sending or receiving Message::Close.

fn read<Stream>(self: &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.

fn write<Stream>(self: &mut Self, stream: &mut Stream, message: Message) -> Result<()>
where
    Stream: Read + Write

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.

fn flush<Stream>(self: &mut Self, stream: &mut Stream) -> Result<()>
where
    Stream: Read + Write

Flush writes.

Ensures all messages previously passed to write and automatically queued pong responses are written & flushed into the stream.

fn close<Stream>(self: &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 send(Message::Close(..)).

impl Debug for WebSocketContext

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Freeze for WebSocketContext

impl RefUnwindSafe for WebSocketContext

impl Send for WebSocketContext

impl Sync for WebSocketContext

impl Unpin for WebSocketContext

impl UnsafeUnpin for WebSocketContext

impl UnwindSafe for WebSocketContext

impl<T> Any for WebSocketContext

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for WebSocketContext

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for WebSocketContext

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for WebSocketContext

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Same for WebSocketContext

impl<T, U> Into for WebSocketContext

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for WebSocketContext

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for WebSocketContext

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<V, T> VZip for WebSocketContext

fn vzip(self: Self) -> V