2021-02-10 13:10:03 +01:00
|
|
|
//! Actix actors support for Actix Web.
|
2022-06-26 18:45:02 +02:00
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
|
|
|
//! use actix::{Actor, StreamHandler};
|
|
|
|
//! use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
|
|
|
//! use actix_web_actors::ws;
|
|
|
|
//!
|
|
|
|
//! /// Define Websocket actor
|
|
|
|
//! struct MyWs;
|
|
|
|
//!
|
|
|
|
//! impl Actor for MyWs {
|
|
|
|
//! type Context = ws::WebsocketContext<Self>;
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! /// Handler for ws::Message message
|
|
|
|
//! impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
|
|
|
|
//! fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
|
|
|
//! match msg {
|
|
|
|
//! Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
|
|
|
|
//! Ok(ws::Message::Text(text)) => ctx.text(text),
|
|
|
|
//! Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
|
|
|
|
//! _ => (),
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[get("/ws")]
|
|
|
|
//! async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
|
|
|
//! ws::start(MyWs, &req, stream)
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[actix_web::main]
|
|
|
|
//! async fn main() -> std::io::Result<()> {
|
|
|
|
//! HttpServer::new(|| App::new().service(index))
|
|
|
|
//! .bind(("127.0.0.1", 8080))?
|
|
|
|
//! .run()
|
|
|
|
//! .await
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! # Documentation & Community Resources
|
|
|
|
//! In addition to this API documentation, several other resources are available:
|
|
|
|
//!
|
|
|
|
//! * [Website & User Guide](https://actix.rs/)
|
|
|
|
//! * [Documentation for `actix_web`](actix_web)
|
|
|
|
//! * [Examples Repository](https://github.com/actix/examples)
|
|
|
|
//! * [Community Chat on Discord](https://discord.gg/NWpN5mmg3x)
|
|
|
|
//!
|
|
|
|
//! To get started navigating the API docs, you may consider looking at the following pages first:
|
|
|
|
//!
|
|
|
|
//! * [`ws`]: This module provides actor support for WebSockets.
|
|
|
|
//!
|
|
|
|
//! * [`HttpContext`]: This struct provides actor support for streaming HTTP responses.
|
|
|
|
//!
|
2020-09-10 13:54:27 +02:00
|
|
|
|
2021-12-08 07:09:56 +01:00
|
|
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
|
|
|
#![warn(future_incompatible)]
|
2023-01-01 21:56:27 +01:00
|
|
|
#![allow(clippy::uninlined_format_args)]
|
2020-09-10 13:54:27 +02:00
|
|
|
|
2019-03-17 21:47:20 +01:00
|
|
|
mod context;
|
2019-03-18 06:11:50 +01:00
|
|
|
pub mod ws;
|
2019-03-17 21:47:20 +01:00
|
|
|
|
|
|
|
pub use self::context::HttpContext;
|