Struct actix_web_actors::HttpContext
source · 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§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
§fn waiting(&self) -> bool
fn waiting(&self) -> bool
Checks if the context is paused (waiting for future completion or stopping).
§fn add_stream<S>(&mut self, fut: S) -> SpawnHandlewhere
S: Stream + 'static,
A: StreamHandler<<S as Stream>::Item>,
fn add_stream<S>(&mut self, fut: S) -> SpawnHandlewhere
S: Stream + 'static,
A: StreamHandler<<S as Stream>::Item>,
Registers a stream with the context. Read more
§fn add_message_stream<S>(&mut self, fut: S)where
S: Stream + 'static,
<S as Stream>::Item: Message,
A: Handler<<S as Stream>::Item>,
fn add_message_stream<S>(&mut self, fut: S)where
S: Stream + 'static,
<S as Stream>::Item: Message,
A: Handler<<S as Stream>::Item>,
Registers a stream with the context, ignoring errors. Read more
§fn notify<M>(&mut self, msg: M)where
A: Handler<M>,
M: Message + 'static,
fn notify<M>(&mut self, msg: M)where
A: Handler<M>,
M: Message + 'static,
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.§fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandlewhere
A: Handler<M>,
M: Message + 'static,
fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandlewhere
A: Handler<M>,
M: Message + 'static,
Sends the message
msg
to self after a specified period of time. Read more§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
§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§impl<A> AsyncContextParts<A> for HttpContext<A>where
A: Actor<Context = Self>,
impl<A> AsyncContextParts<A> for HttpContext<A>where
A: Actor<Context = Self>,
source§impl<A, M> ToEnvelope<A, M> for HttpContext<A>where
A: Actor<Context = HttpContext<A>> + Handler<M>,
M: Message + Send + 'static,
M::Result: Send,
impl<A, M> ToEnvelope<A, M> for HttpContext<A>where
A: Actor<Context = HttpContext<A>> + Handler<M>,
M: Message + Send + 'static,
M::Result: Send,
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