Struct WebSocketContext
pub struct WebSocketContext { /* private fields */ }
Expand description
A context for managing WebSocket stream.
Implementations§
§impl WebSocketContext
impl WebSocketContext
pub fn new(role: Role, config: Option<WebSocketConfig>) -> WebSocketContext
pub fn new(role: Role, config: Option<WebSocketConfig>) -> WebSocketContext
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
.
pub fn from_partially_read(
part: Vec<u8>,
role: Role,
config: Option<WebSocketConfig>,
) -> WebSocketContext
pub fn from_partially_read( part: Vec<u8>, role: Role, config: Option<WebSocketConfig>, ) -> WebSocketContext
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
.
pub 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
.
pub fn get_config(&self) -> &WebSocketConfig
pub fn get_config(&self) -> &WebSocketConfig
Read the configuration.
pub 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.
pub 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
.
pub fn read<Stream>(
&mut self,
stream: &mut Stream,
) -> Result<Message, ProtocolError>
pub fn read<Stream>( &mut self, stream: &mut Stream, ) -> Result<Message, ProtocolError>
Read a message from the provided stream, if possible.
This function sends pong and close responses automatically. However, it never blocks on write.
pub fn write<Stream>(
&mut self,
stream: &mut Stream,
message: Message,
) -> Result<(), ProtocolError>
pub fn write<Stream>( &mut self, stream: &mut Stream, message: Message, ) -> Result<(), ProtocolError>
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.
pub fn flush<Stream>(
&mut self,
stream: &mut Stream,
) -> Result<(), ProtocolError>
pub fn flush<Stream>( &mut self, stream: &mut Stream, ) -> Result<(), ProtocolError>
Flush writes.
Ensures all messages previously passed to write
and automatically
queued pong responses are written & flushed into the stream
.
pub fn close<Stream>(
&mut self,
stream: &mut Stream,
code: Option<CloseFrame>,
) -> Result<(), ProtocolError>
pub fn close<Stream>( &mut self, stream: &mut Stream, code: Option<CloseFrame>, ) -> Result<(), ProtocolError>
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(..))
.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for WebSocketContext
impl RefUnwindSafe for WebSocketContext
impl Send for WebSocketContext
impl Sync for WebSocketContext
impl Unpin for WebSocketContext
impl UnwindSafe for WebSocketContext
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more