From 247c23c1ea5de36e6abe36fc5c0416ad3057ff4a Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 6 Jan 2018 01:06:35 -0800 Subject: [PATCH] no need for StreamHandler --- Cargo.toml | 2 +- examples/state/src/main.rs | 1 - examples/websocket-chat/src/main.rs | 57 ++++++++++++-------------- examples/websocket-chat/src/session.rs | 2 - examples/websocket/src/main.rs | 12 +----- guide/src/qs_9.md | 3 +- src/worker.rs | 3 -- src/ws.rs | 6 +-- 8 files changed, 31 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f3c83ccc..6d831068 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,7 @@ tokio-tls = { version="0.1", optional = true } tokio-openssl = { version="0.1", optional = true } [dependencies.actix] -version = "0.4" +version = "^0.4.1" [dependencies.openssl] version = "0.9" diff --git a/examples/state/src/main.rs b/examples/state/src/main.rs index 6c247329..395007ae 100644 --- a/examples/state/src/main.rs +++ b/examples/state/src/main.rs @@ -36,7 +36,6 @@ impl Actor for MyWebSocket { type Context = HttpContext; } -impl StreamHandler for MyWebSocket {} impl Handler for MyWebSocket { type Result = (); diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index 76cc29e9..633c6556 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -53,6 +53,32 @@ struct WsChatSession { impl Actor for WsChatSession { type Context = HttpContext; + + /// Method is called on actor start. + /// We register ws session with ChatServer + fn started(&mut self, ctx: &mut Self::Context) { + // register self in chat server. `AsyncContext::wait` register + // future within context, but context waits until this future resolves + // before processing any other events. + // HttpContext::state() is instance of WsChatSessionState, state is shared across all + // routes within application + let subs = ctx.sync_subscriber(); + ctx.state().addr.call( + self, server::Connect{addr: subs}).then( + |res, act, ctx| { + match res { + Ok(Ok(res)) => act.id = res, + // something is wrong with chat server + _ => ctx.stop(), + } + fut::ok(()) + }).wait(ctx); + } + + fn stopping(&mut self, ctx: &mut Self::Context) { + // notify chat server + ctx.state().addr.send(server::Disconnect{id: self.id}); + } } /// Handle messages from chat server, we simply send it to peer websocket @@ -143,37 +169,6 @@ impl Handler for WsChatSession { } } -impl StreamHandler for WsChatSession -{ - /// Method is called when stream get polled first time. - /// We register ws session with ChatServer - fn started(&mut self, ctx: &mut Self::Context) { - // register self in chat server. `AsyncContext::wait` register - // future within context, but context waits until this future resolves - // before processing any other events. - // HttpContext::state() is instance of WsChatSessionState, state is shared across all - // routes within application - let subs = ctx.sync_subscriber(); - ctx.state().addr.call( - self, server::Connect{addr: subs}).then( - |res, act, ctx| { - match res { - Ok(Ok(res)) => act.id = res, - // something is wrong with chat server - _ => ctx.stop(), - } - fut::ok(()) - }).wait(ctx); - } - - /// Method is called when stream finishes, even if stream finishes with error. - fn finished(&mut self, ctx: &mut Self::Context) { - // notify chat server - ctx.state().addr.send(server::Disconnect{id: self.id}); - ctx.stop() - } -} - fn main() { let _ = env_logger::init(); let sys = actix::System::new("websocket-example"); diff --git a/examples/websocket-chat/src/session.rs b/examples/websocket-chat/src/session.rs index f7000f41..47c0d0be 100644 --- a/examples/websocket-chat/src/session.rs +++ b/examples/websocket-chat/src/session.rs @@ -185,8 +185,6 @@ impl Actor for TcpServer { struct TcpConnect(TcpStream, net::SocketAddr); /// Handle stream of TcpStream's -impl StreamHandler for TcpServer {} - impl Handler for TcpServer { type Result = (); diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index ba88cf21..fea8929d 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -24,17 +24,7 @@ impl Actor for MyWebSocket { type Context = HttpContext; } -/// Standard actix's stream handler for a stream of `ws::Message` -impl StreamHandler for MyWebSocket { - fn started(&mut self, ctx: &mut Self::Context) { - println!("WebSocket session openned"); - } - - fn finished(&mut self, ctx: &mut Self::Context) { - println!("WebSocket session closed"); - } -} - +/// Handler for `ws::Message` impl Handler for MyWebSocket { type Result = (); diff --git a/guide/src/qs_9.md b/guide/src/qs_9.md index 0c291237..9c45fbd0 100644 --- a/guide/src/qs_9.md +++ b/guide/src/qs_9.md @@ -21,10 +21,9 @@ impl Actor for Ws { } /// Define Handler for ws::Message message -# impl StreamHandler for Ws {} impl Handler for Ws { type Result=(); - + fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext) { match msg { ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg), diff --git a/src/worker.rs b/src/worker.rs index d3bcf88c..bb8190fd 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -151,9 +151,6 @@ impl Actor for Worker where H: HttpHandler + 'static { } } -impl StreamHandler> for Worker - where H: HttpHandler + 'static {} - impl Handler> for Worker where H: HttpHandler + 'static, { diff --git a/src/ws.rs b/src/ws.rs index cd83ea2f..c49cb7d4 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -23,8 +23,6 @@ //! } //! //! // Define Handler for ws::Message message -//! # impl StreamHandler for Ws {} -//! # //! impl Handler for Ws { //! type Result = (); //! @@ -49,7 +47,7 @@ use http::{Method, StatusCode, header}; use bytes::BytesMut; use futures::{Async, Poll, Stream}; -use actix::{Actor, AsyncContext, ResponseType, StreamHandler}; +use actix::{Actor, AsyncContext, ResponseType, Handler}; use payload::ReadAny; use error::{Error, WsHandshakeError}; @@ -86,7 +84,7 @@ impl ResponseType for Message { /// Do websocket handshake and start actor pub fn start(mut req: HttpRequest, actor: A) -> Result - where A: Actor> + StreamHandler, + where A: Actor> + Handler, S: 'static { let mut resp = handshake(&req)?;