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.
Auto Trait Implementations§
impl<'a, A, T> Freeze for WsResponseBuilder<'a, A, T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more