Struct ResponseSender

Source
pub struct ResponseSender { /* private fields */ }
Expand description

Encode Message into Bytes and send it to ResponseStream.

Implementations§

Source§

impl ResponseSender

Source

pub fn downgrade(&self) -> ResponseWeakSender

downgrade Self to a weak sender.

Source

pub fn send( &self, msg: Message, ) -> impl Future<Output = Result<(), ProtocolError>>

encode Message and add to ResponseStream.

Source

pub fn send_error( &self, err: Error, ) -> impl Future<Output = Result<(), ProtocolError>>

add io::Error to ResponseStream.

the error should be used as a signal to the TCP connection associated with ResponseStream to close immediately.

§Examples
use std::{future::poll_fn, pin::Pin, time::Duration};

use futures_core::Stream;
use http_ws::{CloseCode, Message, RequestStream, ResponseSender, ResponseStream};
use tokio::{io::AsyncWriteExt, time::timeout, net::TcpStream};

// thread1:
// read and write websocket message.
async fn sender<S, T, E>(tx: ResponseSender, mut rx: Pin<&mut RequestStream<S>>)
where
    S: Stream<Item = Result<T, E>>,
    T: AsRef<[u8]>,
{
    // send close message to client
    tx.send(Message::Close(Some(CloseCode::Away.into()))).await.unwrap();

    // the client failed to respond to close message in 5 seconds time window.
    if let Err(_) = timeout(Duration::from_secs(5), poll_fn(|cx| rx.as_mut().poll_next(cx))).await {
        // send io error to thread2
        tx.send_error(std::io::ErrorKind::UnexpectedEof.into()).await.unwrap();
    }
}

// thread2:
// receive websocket message from thread1 and transfer it on tcp connection.
async fn io_write(conn: &mut TcpStream, mut rx: Pin<&mut ResponseStream>) {
    // the first message is the "go away" close message in Ok branch.
    let msg = poll_fn(|cx| rx.as_mut().poll_next(cx)).await.unwrap().unwrap();

    // send msg to client
    conn.write_all(&msg).await.unwrap();

    // the second message is the io::Error in Err branch.
    let err = poll_fn(|cx| rx.as_mut().poll_next(cx)).await.unwrap().unwrap_err();

    // at this point we should close the tcp connection by either graceful close or
    // just return immediately and drop the TcpStream.
    let _ = conn.shutdown().await;
}

// thread3:
// receive message from tcp connection and send it to thread1:
async fn io_read(conn: &mut TcpStream) {
    // this part is ignored as it has no relation to the send_error api.
}
Source

pub fn text( &self, txt: impl Into<String>, ) -> impl Future<Output = Result<(), ProtocolError>>

encode Message::Text variant and add to ResponseStream.

Source

pub fn binary( &self, bin: impl Into<Bytes>, ) -> impl Future<Output = Result<(), ProtocolError>>

encode Message::Binary variant and add to ResponseStream.

Source

pub async fn close( self, reason: Option<impl Into<CloseReason>>, ) -> Result<(), ProtocolError>

encode Message::Close variant and add to ResponseStream. take ownership of Self as after close message no more message can be sent to client.

Trait Implementations§

Source§

impl Debug for ResponseSender

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> BorrowState<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more