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, E>>)
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.

Trait Implementations§

source§

impl Debug for ResponseSender

source§

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

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> 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

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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 T
where V: MultiLane<T>,

§

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