pub struct WsResponseBuilder<'a, A, T>
where A: Actor<Context = WebsocketContext<A>> + StreamHandler<Result<Message, ProtocolError>>, T: Stream<Item = Result<Bytes, PayloadError>> + 'static,
{ /* private fields */ }
Expand description

Builder for Websocket session response.

Examples

#[get("/ws")]
async fn websocket(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
    ws::WsResponseBuilder::new(MyWs, &req, stream).start()
}

const MAX_FRAME_SIZE: usize = 16_384; // 16KiB

#[get("/custom-ws")]
async fn custom_websocket(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
    // Create a Websocket session with a specific max frame size, codec, and protocols.
    ws::WsResponseBuilder::new(MyWs, &req, stream)
        .codec(actix_http::ws::Codec::new())
        // This will overwrite the codec's max frame-size
        .frame_size(MAX_FRAME_SIZE)
        .protocols(&["A", "B"])
        .start()
}

Implementations§

source§

impl<'a, A, T> WsResponseBuilder<'a, A, T>
where A: Actor<Context = WebsocketContext<A>> + StreamHandler<Result<Message, ProtocolError>>, T: Stream<Item = Result<Bytes, PayloadError>> + 'static,

source

pub fn new(actor: A, req: &'a HttpRequest, stream: T) -> Self

Construct a new WsResponseBuilder with actor, request, and payload stream.

For usage example, see docs on WsResponseBuilder struct.

source

pub fn protocols(self, protocols: &'a [&'a str]) -> Self

Set the protocols for the session.

source

pub fn frame_size(self, frame_size: usize) -> Self

Set the max frame size for each message (in bytes).

Note: This will override any given Codec’s max frame size.

source

pub fn codec(self, codec: Codec) -> Self

Set the Codec for the session. If Self::frame_size is also set, the given Codec’s max frame size will be overridden.

source

pub fn start(self) -> Result<HttpResponse, Error>

Perform WebSocket handshake and start actor.

req is an HttpRequest that should be requesting a websocket protocol change. stream should be a Bytes stream (such as actix_web::web::Payload) that contains a stream of the body request.

If there is a problem with the handshake, an error is returned.

If successful, consume the WsResponseBuilder and return a HttpResponse wrapped in a Result.

source

pub fn start_with_addr(self) -> Result<(Addr<A>, HttpResponse), Error>

Perform WebSocket handshake and start actor.

req is an HttpRequest that should be requesting a websocket protocol change. stream should be a Bytes stream (such as actix_web::web::Payload) that contains a stream of the body request.

If there is a problem with the handshake, an error is returned.

If successful, returns a pair where the first item is an address for the created actor and the second item is the HttpResponse that should be returned from the websocket request.

Auto Trait Implementations§

§

impl<'a, A, T> !RefUnwindSafe for WsResponseBuilder<'a, A, T>

§

impl<'a, A, T> !Send for WsResponseBuilder<'a, A, T>

§

impl<'a, A, T> !Sync for WsResponseBuilder<'a, A, T>

§

impl<'a, A, T> Unpin for WsResponseBuilder<'a, A, T>
where T: Unpin,

§

impl<'a, A, T> !UnwindSafe for WsResponseBuilder<'a, A, T>

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