diff --git a/Cargo.toml b/Cargo.toml index 890673e6..0f8d6de4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,10 +37,7 @@ members = [ "unix-socket", # "web-cors/backend", # "websocket", -# "websocket-chat", + "websocket-chat", # "websocket-chat-broker", # "websocket-tcp-chat", ] - -[patch.crates-io] -actix-http = { git = "https://github.com/actix/actix-web.git" } diff --git a/diesel/src/main.rs b/diesel/src/main.rs index b2c23523..baed0973 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -60,7 +60,10 @@ async fn add( /// This handler manually parse json object. Bytes object supports FromRequest trait (extractor) /// and could be loaded from request payload automatically -async fn index_add(body: web::Bytes, pool: web::Data) -> Result { +async fn index_add( + body: web::Bytes, + pool: web::Data, +) -> Result { // body is loaded, now we can deserialize id with serde-json let r_obj = serde_json::from_slice::(&body); diff --git a/websocket-chat/Cargo.toml b/websocket-chat/Cargo.toml index fb5817f5..bf542f7a 100644 --- a/websocket-chat/Cargo.toml +++ b/websocket-chat/Cargo.toml @@ -1,8 +1,7 @@ [package] name = "websocket-example" -version = "0.1.0" +version = "2.0.0" authors = ["Nikolay Kim "] -workspace = ".." edition = "2018" [[bin]] @@ -10,16 +9,16 @@ name = "websocket-chat-server" path = "src/main.rs" [dependencies] -actix = "0.8.2" -actix-web = "1.0.0" -actix-web-actors = "1.0.0" -actix-files = "0.1.1" +actix-rt = "1.0.0" +actix = "0.9.0-alpha.1" +actix-web = "2.0.0-alpha.5" +actix-web-actors = "2.0.0-alpha.1" +actix-files = "0.2.0-alpha.3" rand = "0.6" -bytes = "0.4" +bytes = "0.5.3" byteorder = "1.1" -futures = "0.1.25" -tokio-io = "0.1" +futures = "0.3.1" env_logger = "0.6" serde = "1.0" serde_json = "1.0" diff --git a/websocket-chat/src/main.rs b/websocket-chat/src/main.rs index f8eee4c3..4ad583b7 100644 --- a/websocket-chat/src/main.rs +++ b/websocket-chat/src/main.rs @@ -13,7 +13,7 @@ const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const CLIENT_TIMEOUT: Duration = Duration::from_secs(10); /// Entry point for our route -fn chat_route( +async fn chat_route( req: HttpRequest, stream: web::Payload, srv: web::Data>, @@ -71,7 +71,7 @@ impl Actor for WsChatSession { // something is wrong with chat server _ => ctx.stop(), } - fut::ok(()) + fut::ready(()) }) .wait(ctx); } @@ -93,8 +93,20 @@ impl Handler for WsChatSession { } /// WebSocket message handler -impl StreamHandler for WsChatSession { - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { +impl StreamHandler> for WsChatSession { + fn handle( + &mut self, + msg: Result, + ctx: &mut Self::Context, + ) { + let msg = match msg { + Err(_) => { + ctx.stop(); + return; + } + Ok(msg) => msg, + }; + println!("WEBSOCKET MESSAGE: {:?}", msg); match msg { ws::Message::Ping(msg) => { @@ -126,7 +138,7 @@ impl StreamHandler for WsChatSession { } _ => println!("Something is wrong"), } - fut::ok(()) + fut::ready(()) }) .wait(ctx) // .wait(ctx) pauses all events in context, @@ -173,6 +185,9 @@ impl StreamHandler for WsChatSession { ws::Message::Close(_) => { ctx.stop(); } + ws::Message::Continuation(_) => { + ctx.stop(); + } ws::Message::Nop => (), } } @@ -199,14 +214,14 @@ impl WsChatSession { return; } - ctx.ping(""); + ctx.ping(b""); }); } } -fn main() -> std::io::Result<()> { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { env_logger::init(); - let sys = System::new("ws-example"); // Start chat server actor let server = server::ChatServer::default().start(); @@ -227,7 +242,6 @@ fn main() -> std::io::Result<()> { .service(fs::Files::new("/static/", "static/")) }) .bind("127.0.0.1:8080")? - .start(); - - sys.run() + .start() + .await } diff --git a/websocket-chat/src/server.rs b/websocket-chat/src/server.rs index f8acc451..7a9c4c2a 100644 --- a/websocket-chat/src/server.rs +++ b/websocket-chat/src/server.rs @@ -8,6 +8,7 @@ use std::collections::{HashMap, HashSet}; /// Chat server sends this messages to session #[derive(Message)] +#[rtype(result = "()")] pub struct Message(pub String); /// Message for chat server communications @@ -21,12 +22,14 @@ pub struct Connect { /// Session is disconnected #[derive(Message)] +#[rtype(result = "()")] pub struct Disconnect { pub id: usize, } /// Send message to specific room #[derive(Message)] +#[rtype(result = "()")] pub struct ClientMessage { /// Id of the client session pub id: usize, @@ -45,6 +48,7 @@ impl actix::Message for ListRooms { /// Join room, if room does not exists create new one. #[derive(Message)] +#[rtype(result = "()")] pub struct Join { /// Client id pub id: usize,