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§

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

For usage example, see docs on WsResponseBuilder struct.

Set the protocols for the session.

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

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

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

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.

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§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more