[][src]Struct yarws::Socket

pub struct Socket {
    pub no: usize,
    // some fields omitted
}

Represent a WebSocket connection. Used for sending and receiving messages.

Fields

no: usize

Methods

impl Socket[src]

pub async fn recv<'_>(&'_ mut self) -> Option<Msg>[src]

Receives Msg from the other side of the Socket connection. None is returned if the socket is closed.

Examples

Usually used in while loop:

    while let Some(msg) = socket.recv().await {
        // process msg
    }

pub async fn send<'_>(&'_ mut self, msg: Msg) -> Result<(), Error>[src]

Sends Msg to the other side of the Socket connection. Errors if the socket is already closed.

Examples

Echo example. Receive Msgs and replay with the same Msg.

async fn echo(mut socket: Socket) -> Result<(), Error> {
    while let Some(msg) = socket.recv().await {
        socket.send(msg).await?;
    }
    Ok(())
}

pub async fn into_channel(self) -> (Sender<Msg>, Receiver<Msg>)[src]

Transforms Socket into pair of mpsc channels for sending/receiving Msgs.

In some cases it is more convenient to have channels instead of calling methods. Ownership of each side of the channel can be moved to the different function.

pub fn into_text(self) -> TextSocket[src]

Transforms Socket into TextSocket which is more convenient for handling text only messages.

Trait Implementations

impl Debug for Socket[src]

Auto Trait Implementations

impl !RefUnwindSafe for Socket

impl Send for Socket

impl Sync for Socket

impl Unpin for Socket

impl !UnwindSafe for Socket

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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