diff --git a/README.md b/README.md index 84ec0cd5..37fb263e 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Some basic benchmarks could be found in this [respository](https://github.com/fa * [Basic](https://github.com/actix/actix-web/tree/master/examples/basic/) * [Stateful](https://github.com/actix/actix-web/tree/master/examples/state/) * [Mulitpart streams](https://github.com/actix/actix-web/tree/master/examples/multipart/) -* [Simple websocket session](https://github.com/actix/actix-web/tree/master/examples/websocket.rs) +* [Simple websocket session](https://github.com/actix/actix-web/tree/master/examples/websocket/) * [Tera templates](https://github.com/actix/actix-web/tree/master/examples/template_tera/) * [Diesel integration](https://github.com/actix/actix-web/tree/master/examples/diesel/) * [SSL / HTTP/2.0](https://github.com/actix/actix-web/tree/master/examples/tls/) diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml new file mode 100644 index 00000000..3168601a --- /dev/null +++ b/examples/websocket/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "websocket" +version = "0.1.0" +authors = ["Nikolay Kim "] + +[[bin]] +name = "server" +path = "src/main.rs" + +[dependencies] +env_logger = "*" +futures = "0.1" +actix = "^0.3.5" +actix-web = { git = "https://github.com/actix/actix-web.git", features=["signal"] } diff --git a/examples/websocket/README.md b/examples/websocket/README.md new file mode 100644 index 00000000..374e939a --- /dev/null +++ b/examples/websocket/README.md @@ -0,0 +1,27 @@ +# websockect + +Simple echo websocket server. + +## Usage + +### server + +```bash +cd actix-web/examples/websocket +cargo run +# Started http server: 127.0.0.1:8080 +``` + +### web client + +- [http://localhost:8080/ws/index.html](http://localhost:8080/ws/index.html) + +### python client + +- ``pip install aiohttp`` +- ``python websocket-client.py`` + +if ubuntu : + +- ``pip3 install aiohttp`` +- ``python3 websocket-client.py`` diff --git a/examples/websocket.rs b/examples/websocket/src/main.rs similarity index 85% rename from examples/websocket.rs rename to examples/websocket/src/main.rs index 2a80add1..a517a18a 100644 --- a/examples/websocket.rs +++ b/examples/websocket/src/main.rs @@ -10,7 +10,7 @@ extern crate env_logger; use actix::*; use actix_web::*; - +#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe}; /// do websocket handshake and start `MyWebSocket` actor fn ws_index(r: HttpRequest) -> Reply { @@ -60,7 +60,7 @@ fn main() { let _ = env_logger::init(); let sys = actix::System::new("ws-example"); - HttpServer::new( + let _addr = HttpServer::new( || Application::new() // enable logger .middleware(middleware::Logger::default()) @@ -68,11 +68,16 @@ fn main() { .resource("/ws/", |r| r.method(Method::GET).f(ws_index)) // static files .resource("/{tail:.*}", - |r| r.h(fs::StaticFiles::new("tail", "examples/static/", true)))) + |r| r.h(fs::StaticFiles::new("tail", "../static/", true)))) // start http server on 127.0.0.1:8080 .bind("127.0.0.1:8080").unwrap() .start(); + if cfg!(target_os = "linux") { // Subscribe to unix signals + let signals = Arbiter::system_registry().get::(); + signals.send(Subscribe(_addr.subscriber())); + } + println!("Started http server: 127.0.0.1:8080"); let _ = sys.run(); } diff --git a/examples/websocket-client.py b/examples/websocket/websocket-client.py similarity index 100% rename from examples/websocket-client.py rename to examples/websocket/websocket-client.py