pub struct HttpContext<A>where
A: Actor<Context = HttpContext<A>>,{ /* private fields */ }Expand description
Execution context for HTTP actors
§Example
A demonstration of server-sent events using actors:
use std::time::Duration;
use actix::{Actor, AsyncContext};
use actix_web::{get, http::header, App, HttpResponse, HttpServer};
use actix_web_actors::HttpContext;
use bytes::Bytes;
struct MyActor {
count: usize,
}
impl Actor for MyActor {
type Context = HttpContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
ctx.run_later(Duration::from_millis(100), Self::write);
}
}
impl MyActor {
fn write(&mut self, ctx: &mut HttpContext<Self>) {
self.count += 1;
if self.count > 3 {
ctx.write_eof()
} else {
ctx.write(Bytes::from(format!("event: count\ndata: {}\n\n", self.count)));
ctx.run_later(Duration::from_millis(100), Self::write);
}
}
}
#[get("/")]
async fn index() -> HttpResponse {
HttpResponse::Ok()
.insert_header(header::ContentType(mime::TEXT_EVENT_STREAM))
.streaming(HttpContext::create(MyActor { count: 0 }))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()
.await
}Implementations§
Source§impl<A> HttpContext<A>where
A: Actor<Context = Self>,
impl<A> HttpContext<A>where
A: Actor<Context = Self>,
Source§impl<A> HttpContext<A>where
A: Actor<Context = Self>,
impl<A> HttpContext<A>where
A: Actor<Context = Self>,
Trait Implementations§
Source§impl<A> ActorContext for HttpContext<A>where
A: Actor<Context = Self>,
impl<A> ActorContext for HttpContext<A>where
A: Actor<Context = Self>,
Source§fn stop(&mut self)
fn stop(&mut self)
Immediately stop processing incoming messages and switch to a
stopping state. This only affects actors that are currently
running. Future attempts to queue messages will fail.Source§fn terminate(&mut self)
fn terminate(&mut self)
Terminate actor execution unconditionally. This sets the actor
into the
stopped state. This causes future attempts to queue
messages to fail.Source§fn state(&self) -> ActorState
fn state(&self) -> ActorState
Retrieve the current Actor execution state.
Source§impl<A> AsyncContext<A> for HttpContext<A>where
A: Actor<Context = Self>,
impl<A> AsyncContext<A> for HttpContext<A>where
A: Actor<Context = Self>,
Source§fn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
F: ActorFuture<A, Output = ()> + 'static,
fn spawn<F>(&mut self, fut: F) -> SpawnHandlewhere
F: ActorFuture<A, Output = ()> + 'static,
Spawns a future into the context. Read more
Source§fn wait<F>(&mut self, fut: F)where
F: ActorFuture<A, Output = ()> + 'static,
fn wait<F>(&mut self, fut: F)where
F: ActorFuture<A, Output = ()> + 'static,
Spawns a future into the context, waiting for it to resolve. Read more
Source§fn cancel_future(&mut self, handle: SpawnHandle) -> bool
fn cancel_future(&mut self, handle: SpawnHandle) -> bool
Cancels a spawned future. Read more
Source§fn waiting(&self) -> bool
fn waiting(&self) -> bool
Checks if the context is paused (waiting for future completion or stopping).
Source§fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
Registers a stream with the context. Read more
Source§fn add_message_stream<S>(&mut self, fut: S)
fn add_message_stream<S>(&mut self, fut: S)
Registers a stream with the context, ignoring errors. Read more
Source§fn notify<M>(&mut self, msg: M)
fn notify<M>(&mut self, msg: M)
Sends the message
msg to self. This bypasses the mailbox capacity, and
will always queue the message. If the actor is in the stopped state, an
error will be raised.Source§fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
Sends the message
msg to self after a specified period of time. Read moreSource§fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
Executes a closure after a specified period of time. Read more
Source§fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
Spawns a job to execute the given closure periodically, at a
specified fixed interval.
Source§fn run_interval_at<F>(
&mut self,
start: Instant,
interval: Duration,
task: F,
) -> SpawnHandle
fn run_interval_at<F>( &mut self, start: Instant, interval: Duration, task: F, ) -> SpawnHandle
Spawns a periodic
task function to begin executing at the given start time, and with the
given interval duration.Source§impl<A> AsyncContextParts<A> for HttpContext<A>where
A: Actor<Context = Self>,
impl<A> AsyncContextParts<A> for HttpContext<A>where
A: Actor<Context = Self>,
fn parts(&mut self) -> &mut ContextParts<A>
Auto Trait Implementations§
impl<A> Freeze for HttpContext<A>
impl<A> !RefUnwindSafe for HttpContext<A>
impl<A> !Send for HttpContext<A>
impl<A> !Sync for HttpContext<A>
impl<A> Unpin for HttpContext<A>
impl<A> !UnwindSafe for HttpContext<A>
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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