From 1779f963d9c77b46a09dd2b6ae720b89760ff09b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 18 Mar 2019 05:31:32 -0700 Subject: [PATCH] migrate websocket example --- websocket/Cargo.toml | 19 ++++++++++-------- websocket/src/main.rs | 45 +++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/websocket/Cargo.toml b/websocket/Cargo.toml index 4ed656ec..a3f662c0 100644 --- a/websocket/Cargo.toml +++ b/websocket/Cargo.toml @@ -2,19 +2,22 @@ name = "websocket" version = "0.1.0" authors = ["Nikolay Kim "] -workspace = "../" +edition = "2018" +workspace = ".." [[bin]] name = "websocket-server" path = "src/main.rs" -[[bin]] -name = "websocket-client" -path = "src/client.rs" +#[[bin]] +#name = "websocket-client" +#path = "src/client.rs" [dependencies] -env_logger = "*" +actix = { git="https://github.com/actix/actix.git" } +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-web-actors = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +actix-files = { git="https://github.com/actix/actix-web.git", branch = "1.0" } +env_logger = "0.6" futures = "0.1" - -actix = "0.7" -actix-web = "0.7" +bytes = "0.4" \ No newline at end of file diff --git a/websocket/src/main.rs b/websocket/src/main.rs index 0b9a5716..25aac6ab 100644 --- a/websocket/src/main.rs +++ b/websocket/src/main.rs @@ -3,17 +3,16 @@ //! or [python console client](https://github.com/actix/examples/blob/master/websocket/websocket-client.py) //! could be used for testing. -#![allow(unused_variables)] -extern crate actix; -extern crate actix_web; -extern crate env_logger; - use std::time::{Duration, Instant}; use actix::prelude::*; +use actix_files as fs; use actix_web::{ - fs, http, middleware, server, ws, App, Error, HttpRequest, HttpResponse, + error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer, }; +use actix_web_actors::ws; +use bytes::Bytes; +use futures::Stream; /// How often heartbeat pings are sent const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); @@ -21,8 +20,14 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); /// do websocket handshake and start `MyWebSocket` actor -fn ws_index(r: &HttpRequest) -> Result { - ws::start(r, MyWebSocket::new()) +fn ws_index(r: HttpRequest, stream: web::Payload) -> Result +where + S: Stream + 'static, +{ + println!("{:?}", r); + let res = ws::start(MyWebSocket::new(), &r, stream); + println!("{:?}", res.as_ref().unwrap()); + res } /// websocket connection is long running connection, it easier @@ -91,30 +96,20 @@ impl MyWebSocket { } } -fn main() { - ::std::env::set_var("RUST_LOG", "actix_web=info"); +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); env_logger::init(); - let sys = actix::System::new("ws-example"); - server::new(|| { + HttpServer::new(|| { App::new() // enable logger .middleware(middleware::Logger::default()) // websocket route - .resource("/ws/", |r| r.method(http::Method::GET).f(ws_index)) + .service(web::resource("/ws/").route(web::get().to(ws_index))) // static files - .handler( - "/", - fs::StaticFiles::new("static/") - .unwrap() - .index_file("index.html"), - ) + .service(fs::Files::new("/", "static/").index_file("index.html")) }) // start http server on 127.0.0.1:8080 - .bind("127.0.0.1:8080") - .unwrap() - .start(); - - println!("Started http server: 127.0.0.1:8080"); - let _ = sys.run(); + .bind("127.0.0.1:8080")? + .run() }