Struct web_socket::WebSocket

source ·
pub struct WebSocket<Stream> {
    pub stream: Stream,
    pub max_payload_len: usize,
    /* private fields */
}
Expand description

WebSocket implementation for both client and server

Fields§

§stream: Stream

it is a low-level abstraction that represents the underlying byte stream over which WebSocket messages are exchanged.

§max_payload_len: usize

Maximum allowed payload length in bytes.

Default: 16 MB

Implementations§

source§

impl<IO> WebSocket<IO>

source

pub fn client(stream: IO) -> Self

Create a new websocket client instance.

source

pub fn server(stream: IO) -> Self

Create a websocket server instance.

source§

impl<W> WebSocket<W>where W: Unpin + AsyncWrite,

source

pub async fn send(&mut self, data: impl Into<Frame<'_>>) -> Result<()>

Send message to a endpoint.

source

pub async fn close<T>(self, reason: T) -> Result<()>where T: CloseReason, T::Bytes: AsRef<[u8]>,

  • The Close frame MAY contain a body that indicates a reason for closing.
source

pub async fn send_ping(&mut self, data: impl AsRef<[u8]>) -> Result<()>

A Ping frame may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive.

It is used to send ping frame.

Example
let writer = Vec::new();
let mut ws = WebSocket::client(writer);
ws.send_ping("Hello!").await;
source

pub async fn send_pong(&mut self, data: impl AsRef<[u8]>) -> Result<()>

A Pong frame sent in response to a Ping frame must have identical “Application data” as found in the message body of the Ping frame being replied to.

A Pong frame MAY be sent unsolicited. This serves as a unidirectional heartbeat. A response to an unsolicited Pong frame is not expected.

source

pub async fn flash(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.

source§

impl<R> WebSocket<R>where R: Unpin + AsyncRead,

source

pub async fn recv(&mut self) -> Result<Event>

reads Event from websocket stream.

source

pub async fn recv_event(&mut self) -> Result<Event>

reads Event from websocket stream.

Trait Implementations§

source§

impl<Stream: Debug> Debug for WebSocket<Stream>

source§

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

Formats the value using the given formatter. Read more
source§

impl<IO> From<(IO, Role)> for WebSocket<IO>

source§

fn from((stream, role): (IO, Role)) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Stream> RefUnwindSafe for WebSocket<Stream>where Stream: RefUnwindSafe,

§

impl<Stream> Send for WebSocket<Stream>where Stream: Send,

§

impl<Stream> Sync for WebSocket<Stream>where Stream: Sync,

§

impl<Stream> Unpin for WebSocket<Stream>where Stream: Unpin,

§

impl<Stream> UnwindSafe for WebSocket<Stream>where Stream: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V