Struct actix_web_actors::ws::WsResponseBuilder
source · 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,
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,
sourcepub fn new(actor: A, req: &'a HttpRequest, stream: T) -> Self
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.
sourcepub fn frame_size(self, frame_size: usize) -> Self
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.
sourcepub fn codec(self, codec: Codec) -> Self
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.
sourcepub fn start(self) -> Result<HttpResponse, Error>
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
.
sourcepub fn start_with_addr(self) -> Result<(Addr<A>, HttpResponse), Error>
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.