From 5ae646332e9c9efbebf41d643cfd4e894641ec02 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 5 Jan 2018 14:01:19 -0800 Subject: [PATCH] update example to use actix 0.4 --- examples/basics/src/main.rs | 1 - examples/diesel/src/db.rs | 8 +- examples/diesel/src/main.rs | 7 +- examples/diesel/test.db | Bin 20480 -> 20480 bytes examples/json/src/main.rs | 11 +- examples/multipart/src/main.rs | 8 +- examples/signals/src/main.rs | 14 +-- examples/state/src/main.rs | 15 +-- examples/template_tera/src/main.rs | 10 +- examples/tls/src/main.rs | 9 +- examples/websocket-chat/src/client.rs | 74 +++++------- examples/websocket-chat/src/main.rs | 23 ++-- examples/websocket-chat/src/server.rs | 47 +++----- examples/websocket-chat/src/session.rs | 151 ++++++++++--------------- examples/websocket/src/main.rs | 15 +-- 15 files changed, 150 insertions(+), 243 deletions(-) diff --git a/examples/basics/src/main.rs b/examples/basics/src/main.rs index c1d4902e2..b0674c8dd 100644 --- a/examples/basics/src/main.rs +++ b/examples/basics/src/main.rs @@ -7,7 +7,6 @@ extern crate env_logger; extern crate futures; use futures::Stream; -use actix::*; use actix_web::*; use actix_web::middleware::RequestSession; use futures::future::{FutureResult, result}; diff --git a/examples/diesel/src/db.rs b/examples/diesel/src/db.rs index 4e7bced91..04daa6ed0 100644 --- a/examples/diesel/src/db.rs +++ b/examples/diesel/src/db.rs @@ -27,9 +27,9 @@ impl Actor for DbExecutor { } impl Handler for DbExecutor { - fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) - -> Response - { + type Result = MessageResult; + + fn handle(&mut self, msg: CreateUser, _: &mut Self::Context) -> Self::Result { use self::schema::users::dsl::*; let uuid = format!("{}", uuid::Uuid::new_v4()); @@ -48,6 +48,6 @@ impl Handler for DbExecutor { .load::(&self.0) .expect("Error loading person"); - Self::reply(items.pop().unwrap()) + Ok(items.pop().unwrap()) } } diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs index 09e8424d1..6c3170ede 100644 --- a/examples/diesel/src/main.rs +++ b/examples/diesel/src/main.rs @@ -18,8 +18,6 @@ extern crate env_logger; use actix::*; use actix_web::*; -use actix::prelude::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; use diesel::prelude::*; @@ -72,9 +70,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(_addr.subscriber())); } + let signals = actix::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/diesel/test.db b/examples/diesel/test.db index 3afa1579ec71d061fd73a4bfbb498d853f58d024..65e590a6e5f8f16622eee5da4c64c5a18f7b3423 100644 GIT binary patch delta 147 zcmZozz}T>Wae_1>>qHr6M%Il9OZ2&z`O6siFY_Wae_1>%S0JxMwX2UOY}LI_!l$qU*=!DSx{g)|K!E?i9nI{4E*c)ZvaIW K^G{yy9|!==_Ypw= diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index 3d1338135..462826393 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -7,9 +7,7 @@ extern crate serde_json; #[macro_use] extern crate serde_derive; #[macro_use] extern crate json; -use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; use bytes::BytesMut; @@ -23,7 +21,7 @@ struct MyObj { } /// This handler uses `HttpRequest::json()` for loading serde json object. -fn index(mut req: HttpRequest) -> Box> { +fn index(req: HttpRequest) -> Box> { req.json() .from_err() // convert all errors into `Error` .and_then(|val: MyObj| { @@ -98,11 +96,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::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/multipart/src/main.rs b/examples/multipart/src/main.rs index 38b6d5325..965cc82c6 100644 --- a/examples/multipart/src/main.rs +++ b/examples/multipart/src/main.rs @@ -6,7 +6,6 @@ extern crate futures; use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; use futures::{Future, Stream}; @@ -57,11 +56,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::Arbiter::system_registry().get::(); + signals.send(Subscribe(addr.subscriber())); println!("Starting http server: 127.0.0.1:8080"); let _ = sys.run(); diff --git a/examples/signals/src/main.rs b/examples/signals/src/main.rs index d010732e7..92ceb5a8a 100644 --- a/examples/signals/src/main.rs +++ b/examples/signals/src/main.rs @@ -5,7 +5,6 @@ extern crate env_logger; use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; struct MyWebSocket; @@ -16,8 +15,10 @@ impl Actor for MyWebSocket { impl StreamHandler for MyWebSocket {} impl Handler for MyWebSocket { - fn handle(&mut self, _: ws::Message, _: &mut Self::Context) -> Response { - Self::empty() + type Result = (); + + fn handle(&mut self, _: ws::Message, _: &mut Self::Context) { + {} } } @@ -36,11 +37,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::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/state/src/main.rs b/examples/state/src/main.rs index 526657ed2..e60e7d706 100644 --- a/examples/state/src/main.rs +++ b/examples/state/src/main.rs @@ -11,7 +11,6 @@ use std::cell::Cell; use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; /// Application state @@ -40,9 +39,9 @@ impl Actor for MyWebSocket { impl StreamHandler for MyWebSocket {} impl Handler for MyWebSocket { - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) - -> Response - { + type Result = (); + + fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { self.counter += 1; println!("WS({}): {:?}", self.counter, msg); match msg { @@ -54,7 +53,6 @@ impl Handler for MyWebSocket { } _ => (), } - Self::empty() } } @@ -77,11 +75,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::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/template_tera/src/main.rs b/examples/template_tera/src/main.rs index ebd3d3ae5..28706217f 100644 --- a/examples/template_tera/src/main.rs +++ b/examples/template_tera/src/main.rs @@ -4,9 +4,7 @@ extern crate env_logger; #[macro_use] extern crate tera; -use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; @@ -45,11 +43,9 @@ fn main() { .bind("127.0.0.1:8080").unwrap() .start(); - #[cfg(unix)] - { // Subscribe to unix signals - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + // Subscribe to unix signals + let signals = actix::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/tls/src/main.rs b/examples/tls/src/main.rs index 6210a78f0..ad0d4b23b 100644 --- a/examples/tls/src/main.rs +++ b/examples/tls/src/main.rs @@ -6,9 +6,7 @@ extern crate env_logger; use std::fs::File; use std::io::Read; -use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; /// somple handle @@ -49,11 +47,8 @@ fn main() { .start_ssl(&pkcs12).unwrap(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::Arbiter::system_registry().get::(); + signals.send(Subscribe(addr.subscriber())); println!("Started http server: 127.0.0.1:8443"); let _ = sys.run(); diff --git a/examples/websocket-chat/src/client.rs b/examples/websocket-chat/src/client.rs index c46a4875c..f1e52e051 100644 --- a/examples/websocket-chat/src/client.rs +++ b/examples/websocket-chat/src/client.rs @@ -1,4 +1,4 @@ -extern crate actix; +#[macro_use] extern crate actix; extern crate bytes; extern crate byteorder; extern crate futures; @@ -56,13 +56,9 @@ fn main() { struct ChatClient; +#[derive(Message)] struct ClientCommand(String); -impl ResponseType for ClientCommand { - type Item = (); - type Error = (); -} - impl Actor for ChatClient { type Context = FramedContext; @@ -70,6 +66,13 @@ impl Actor for ChatClient { // start heartbeats otherwise server will disconnect after 10 seconds self.hb(ctx) } + + fn stopping(&mut self, _: &mut FramedContext) { + println!("Disconnected"); + + // Stop application on disconnect + Arbiter::system().send(actix::msgs::SystemExit(0)); + } } impl ChatClient { @@ -83,14 +86,13 @@ impl ChatClient { } /// Handle stdin commands -impl Handler for ChatClient -{ - fn handle(&mut self, msg: ClientCommand, ctx: &mut FramedContext) - -> Response - { +impl Handler for ChatClient { + type Result = (); + + fn handle(&mut self, msg: ClientCommand, ctx: &mut FramedContext) { let m = msg.0.trim(); if m.is_empty() { - return Self::empty() + return } // we check for /sss type of messages @@ -112,8 +114,6 @@ impl Handler for ChatClient } else { let _ = ctx.send(codec::ChatRequest::Message(m.to_owned())); } - - Self::empty() } } @@ -122,40 +122,26 @@ impl Handler for ChatClient impl FramedActor for ChatClient { type Io = TcpStream; type Codec = codec::ClientChatCodec; -} -impl StreamHandler for ChatClient { - - fn finished(&mut self, _: &mut FramedContext) { - println!("Disconnected"); - - // Stop application on disconnect - Arbiter::system().send(msgs::SystemExit(0)); - } -} - -impl Handler for ChatClient { - - fn handle(&mut self, msg: codec::ChatResponse, _: &mut FramedContext) - -> Response - { + fn handle(&mut self, msg: io::Result, ctx: &mut FramedContext) { match msg { - codec::ChatResponse::Message(ref msg) => { - println!("message: {}", msg); - } - codec::ChatResponse::Joined(ref msg) => { - println!("!!! joined: {}", msg); - } - codec::ChatResponse::Rooms(rooms) => { - println!("\n!!! Available rooms:"); - for room in rooms { - println!("{}", room); + Err(_) => ctx.stop(), + Ok(msg) => match msg { + codec::ChatResponse::Message(ref msg) => { + println!("message: {}", msg); } - println!(""); + codec::ChatResponse::Joined(ref msg) => { + println!("!!! joined: {}", msg); + } + codec::ChatResponse::Rooms(rooms) => { + println!("\n!!! Available rooms:"); + for room in rooms { + println!("{}", room); + } + println!(""); + } + _ => (), } - _ => (), } - - Self::empty() } } diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index 78fbcdf3c..39e7978f5 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -10,6 +10,7 @@ extern crate serde; extern crate serde_json; #[macro_use] extern crate serde_derive; +#[macro_use] extern crate actix; extern crate actix_web; @@ -17,7 +18,6 @@ use std::time::Instant; use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; mod codec; @@ -58,19 +58,18 @@ impl Actor for WsChatSession { /// Handle messages from chat server, we simply send it to peer websocket impl Handler for WsChatSession { - fn handle(&mut self, msg: session::Message, ctx: &mut Self::Context) - -> Response - { + type Result = (); + + fn handle(&mut self, msg: session::Message, ctx: &mut Self::Context) { ws::WsWriter::text(ctx, &msg.0); - Self::empty() } } /// WebSocket message handler impl Handler for WsChatSession { - fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) - -> Response - { + type Result = (); + + fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { println!("WEBSOCKET MESSAGE: {:?}", msg); match msg { ws::Message::Ping(msg) => @@ -142,7 +141,6 @@ impl Handler for WsChatSession { } _ => (), } - Self::empty() } } @@ -216,11 +214,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(addr.subscriber())); - } + let signals = actix::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-chat/src/server.rs b/examples/websocket-chat/src/server.rs index c15644a6a..4eae86d00 100644 --- a/examples/websocket-chat/src/server.rs +++ b/examples/websocket-chat/src/server.rs @@ -13,7 +13,7 @@ use session; /// New chat session is created pub struct Connect { - pub addr: Box + Send>, + pub addr: Box + Send>, } /// Response type for Connect message @@ -25,16 +25,13 @@ impl ResponseType for Connect { } /// Session is disconnected +#[derive(Message)] pub struct Disconnect { pub id: usize, } -impl ResponseType for Disconnect { - type Item = (); - type Error = (); -} - /// Send message to specific room +#[derive(Message)] pub struct Message { /// Id of the client session pub id: usize, @@ -44,11 +41,6 @@ pub struct Message { pub room: String, } -impl ResponseType for Message { - type Item = (); - type Error = (); -} - /// List of available rooms pub struct ListRooms; @@ -58,6 +50,7 @@ impl ResponseType for ListRooms { } /// Join room, if room does not exists create new one. +#[derive(Message)] pub struct Join { /// Client id pub id: usize, @@ -65,15 +58,10 @@ pub struct Join { pub name: String, } -impl ResponseType for Join { - type Item = (); - type Error = (); -} - /// `ChatServer` manages chat rooms and responsible for coordinating chat session. /// implementation is super primitive pub struct ChatServer { - sessions: HashMap + Send>>, + sessions: HashMap + Send>>, rooms: HashMap>, rng: RefCell, } @@ -118,8 +106,9 @@ impl Actor for ChatServer { /// /// Register new session and assign unique id to this session impl Handler for ChatServer { + type Result = MessageResult; - fn handle(&mut self, msg: Connect, _: &mut Context) -> Response { + fn handle(&mut self, msg: Connect, _: &mut Context) -> Self::Result { println!("Someone joined"); // notify all users in same room @@ -133,14 +122,15 @@ impl Handler for ChatServer { self.rooms.get_mut(&"Main".to_owned()).unwrap().insert(id); // send id back - Self::reply(id) + Ok(id) } } /// Handler for Disconnect message. impl Handler for ChatServer { + type Result = (); - fn handle(&mut self, msg: Disconnect, _: &mut Context) -> Response { + fn handle(&mut self, msg: Disconnect, _: &mut Context) { println!("Someone disconnected"); let mut rooms: Vec = Vec::new(); @@ -158,40 +148,39 @@ impl Handler for ChatServer { for room in rooms { self.send_message(&room, "Someone disconnected", 0); } - - Self::empty() } } /// Handler for Message message. impl Handler for ChatServer { + type Result = (); - fn handle(&mut self, msg: Message, _: &mut Context) -> Response { + fn handle(&mut self, msg: Message, _: &mut Context) { self.send_message(&msg.room, msg.msg.as_str(), msg.id); - - Self::empty() } } /// Handler for `ListRooms` message. impl Handler for ChatServer { + type Result = MessageResult; - fn handle(&mut self, _: ListRooms, _: &mut Context) -> Response { + fn handle(&mut self, _: ListRooms, _: &mut Context) -> Self::Result { let mut rooms = Vec::new(); for key in self.rooms.keys() { rooms.push(key.to_owned()) } - Self::reply(rooms) + Ok(rooms) } } /// Join room, send disconnect message to old room /// send join message to new room impl Handler for ChatServer { + type Result = (); - fn handle(&mut self, msg: Join, _: &mut Context) -> Response { + fn handle(&mut self, msg: Join, _: &mut Context) { let Join {id, name} = msg; let mut rooms = Vec::new(); @@ -211,7 +200,5 @@ impl Handler for ChatServer { } self.send_message(&name, "Someone connected", id); self.rooms.get_mut(&name).unwrap().insert(id); - - Self::empty() } } diff --git a/examples/websocket-chat/src/session.rs b/examples/websocket-chat/src/session.rs index 961955a59..f7000f416 100644 --- a/examples/websocket-chat/src/session.rs +++ b/examples/websocket-chat/src/session.rs @@ -6,20 +6,16 @@ use std::time::{Instant, Duration}; use futures::Stream; use tokio_core::net::{TcpStream, TcpListener}; -use actix::*; +use actix::prelude::*; use server::{self, ChatServer}; use codec::{ChatRequest, ChatResponse, ChatCodec}; /// Chat server sends this messages to session +#[derive(Message)] pub struct Message(pub String); -impl ResponseType for Message { - type Item = (); - type Error = (); -} - /// `ChatSession` actor is responsible for tcp peer communitions. pub struct ChatSession { /// unique session id @@ -36,104 +32,87 @@ impl Actor for ChatSession { /// For tcp communication we are going to use `FramedContext`. /// It is convinient wrapper around `Framed` object from `tokio_io` type Context = FramedContext; -} -/// To use `FramedContext` we have to define Io type and Codec -impl FramedActor for ChatSession { - type Io = TcpStream; - type Codec= ChatCodec; -} - -/// Also `FramedContext` requires Actor which is able to handle stream -/// of `::Item` items. -impl StreamHandler for ChatSession { - - fn started(&mut self, ctx: &mut FramedContext) { + fn started(&mut self, ctx: &mut Self::Context) { // we'll start heartbeat process on session start. self.hb(ctx); // register self in chat server. `AsyncContext::wait` register // future within context, but context waits until this future resolves // before processing any other events. - self.addr.call(self, server::Connect{addr: ctx.sync_subscriber()}).then(|res, act, ctx| { - match res { - Ok(Ok(res)) => act.id = res, - // something is wrong with chat server - _ => ctx.stop(), - } - fut::ok(()) - }).wait(ctx); + let addr: SyncAddress<_> = ctx.address(); + self.addr.call(self, server::Connect{addr: addr.subscriber()}) + .then(|res, act, ctx| { + match res { + Ok(Ok(res)) => act.id = res, + // something is wrong with chat server + _ => ctx.stop(), + } + actix::fut::ok(()) + }).wait(ctx); } - fn finished(&mut self, ctx: &mut FramedContext) { + fn stopping(&mut self, ctx: &mut Self::Context) { // notify chat server self.addr.send(server::Disconnect{id: self.id}); - ctx.stop() } } -impl Handler for ChatSession { - - /// We'll stop chat session actor on any error, high likely it is just - /// termination of the tcp stream. - fn error(&mut self, _: io::Error, ctx: &mut FramedContext) { - ctx.stop() - } +/// To use `FramedContext` we have to define Io type and Codec +impl FramedActor for ChatSession { + type Io = TcpStream; + type Codec= ChatCodec; /// This is main event loop for client requests - fn handle(&mut self, msg: ChatRequest, ctx: &mut FramedContext) - -> Response - { + fn handle(&mut self, msg: io::Result, ctx: &mut FramedContext) { match msg { - ChatRequest::List => { - // Send ListRooms message to chat server and wait for response - println!("List rooms"); - self.addr.call(self, server::ListRooms).then(|res, _, ctx| { - match res { - Ok(Ok(rooms)) => { - let _ = ctx.send(ChatResponse::Rooms(rooms)); - }, + Err(_) => ctx.stop(), + Ok(msg) => match msg { + ChatRequest::List => { + // Send ListRooms message to chat server and wait for response + println!("List rooms"); + self.addr.call(self, server::ListRooms).then(|res, _, ctx| { + match res { + Ok(Ok(rooms)) => { + let _ = ctx.send(ChatResponse::Rooms(rooms)); + }, _ => println!("Something is wrong"), - } - fut::ok(()) - }).wait(ctx) - // .wait(ctx) pauses all events in context, - // so actor wont receive any new messages until it get list of rooms back - }, - ChatRequest::Join(name) => { - println!("Join to room: {}", name); - self.room = name.clone(); - self.addr.send(server::Join{id: self.id, name: name.clone()}); - let _ = ctx.send(ChatResponse::Joined(name)); - }, - ChatRequest::Message(message) => { - // send message to chat server - println!("Peer message: {}", message); - self.addr.send( - server::Message{id: self.id, - msg: message, room: - self.room.clone()}) + } + actix::fut::ok(()) + }).wait(ctx) + // .wait(ctx) pauses all events in context, + // so actor wont receive any new messages until it get list of rooms back + }, + ChatRequest::Join(name) => { + println!("Join to room: {}", name); + self.room = name.clone(); + self.addr.send(server::Join{id: self.id, name: name.clone()}); + let _ = ctx.send(ChatResponse::Joined(name)); + }, + ChatRequest::Message(message) => { + // send message to chat server + println!("Peer message: {}", message); + self.addr.send( + server::Message{id: self.id, + msg: message, room: + self.room.clone()}) + } + // we update heartbeat time on ping from peer + ChatRequest::Ping => + self.hb = Instant::now(), } - // we update heartbeat time on ping from peer - ChatRequest::Ping => - self.hb = Instant::now(), } - - Self::empty() } } /// Handler for Message, chat server sends this message, we just send string to peer impl Handler for ChatSession { + type Result = (); - fn handle(&mut self, msg: Message, ctx: &mut FramedContext) - -> Response - { + fn handle(&mut self, msg: Message, ctx: &mut FramedContext) { // send message to peer let _ = ctx.send(ChatResponse::Message(msg.0)); - - Self::empty() } } @@ -188,7 +167,9 @@ impl TcpServer { // So to be able to handle this events `Server` actor has to implement // stream handler `StreamHandler<(TcpStream, net::SocketAddr), io::Error>` let _: () = TcpServer::create(|ctx| { - ctx.add_stream(listener.incoming().map(|(t, a)| TcpConnect(t, a))); + ctx.add_message_stream(listener.incoming() + .map_err(|_| ()) + .map(|(t, a)| TcpConnect(t, a))); TcpServer{chat: chat} }); } @@ -200,27 +181,19 @@ impl Actor for TcpServer { type Context = Context; } +#[derive(Message)] struct TcpConnect(TcpStream, net::SocketAddr); -impl ResponseType for TcpConnect { - type Item = (); - type Error = (); -} - /// Handle stream of TcpStream's -impl StreamHandler for TcpServer {} +impl StreamHandler for TcpServer {} -impl Handler for TcpServer { +impl Handler for TcpServer { + type Result = (); - fn handle(&mut self, msg: TcpConnect, _: &mut Context) -> Response - { + fn handle(&mut self, msg: TcpConnect, _: &mut Context) { // For each incoming connection we create `ChatSession` actor // with out chat server address. let server = self.chat.clone(); let _: () = ChatSession::new(server).framed(msg.0, ChatCodec); - - // this is response for message, which is defined by `ResponseType` trait - // in this case we just return unit. - Self::empty() } } diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 035df38ea..b7433203f 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -10,7 +10,6 @@ extern crate env_logger; use actix::*; use actix_web::*; -#[cfg(unix)] use actix::actors::signal::{ProcessSignals, Subscribe}; /// do websocket handshake and start `MyWebSocket` actor @@ -38,9 +37,9 @@ impl StreamHandler for MyWebSocket { } impl Handler for MyWebSocket { - fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext) - -> Response - { + type Result = (); + + fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext) { // process websocket messages println!("WS: {:?}", msg); match msg { @@ -52,7 +51,6 @@ impl Handler for MyWebSocket { } _ => (), } - Self::empty() } } @@ -74,11 +72,8 @@ fn main() { .start(); // Subscribe to unix signals - #[cfg(unix)] - { - let signals = actix::Arbiter::system_registry().get::(); - signals.send(Subscribe(_addr.subscriber())); - } + let signals = actix::Arbiter::system_registry().get::(); + signals.send(Subscribe(_addr.subscriber())); println!("Started http server: 127.0.0.1:8080"); let _ = sys.run();