diff --git a/content/docs/websockets.md b/content/docs/websockets.md index b284a36..c0335cd 100644 --- a/content/docs/websockets.md +++ b/content/docs/websockets.md @@ -12,36 +12,7 @@ with an http actor. The following is an example of a simple websocket echo server: -```rust -use actix::*; -use actix_web::*; - -/// Define http actor -struct Ws; - -impl Actor for Ws { - type Context = ws::WebsocketContext; -} - -/// Handler for ws::Message message -impl StreamHandler for Ws { - - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { - match msg { - ws::Message::Ping(msg) => ctx.pong(&msg), - ws::Message::Text(text) => ctx.text(text), - ws::Message::Binary(bin) => ctx.binary(bin), - _ => (), - } - } -} - -fn main() { - App::new() - .resource("/ws/", |r| r.f(|req| ws::start(req, Ws))) - .finish(); -} -``` +{{< include-example example="websockets" file="main.rs" section="websockets" >}} > A simple websocket echo server example is available in the > [examples directory](https://github.com/actix/examples/tree/master/websocket/). diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 981e7a6..72e0df5 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -23,4 +23,5 @@ exclude = [ "testing", "middleware", "static-files", + "websockets", ] diff --git a/examples/websockets/Cargo.toml b/examples/websockets/Cargo.toml new file mode 100644 index 0000000..a4f99be --- /dev/null +++ b/examples/websockets/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "websockets" +version = "0.1.0" +authors = ["Cameron Dershem "] +edition = "2018" + +[dependencies] +actix = "0.7" +actix-web = "1.0" diff --git a/examples/websockets/src/main.rs b/examples/websockets/src/main.rs new file mode 100644 index 0000000..6cec49d --- /dev/null +++ b/examples/websockets/src/main.rs @@ -0,0 +1,29 @@ +// +// use actix::*; +// use actix_web::*; + +// /// Define http actor +// struct Ws; + +// impl Actor for Ws { +// type Context = ws::WebsocketContext; +// } + +// /// Handler for ws::Message message +// impl StreamHandler for Ws { +// fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { +// match msg { +// ws::Message::Ping(msg) => ctx.pong(&msg), +// ws::Message::Text(text) => ctx.text(text), +// ws::Message::Binary(bin) => ctx.binary(bin), +// _ => (), +// } +// } +// } + +// fn main() { +// App::new() +// .resource("/ws/", |r| r.f(|req| ws::start(req, Ws))) +// .finish(); +// } +//