From 9835a4537a4cddc0f77832a15c67210848467edd Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 28 Jan 2018 08:58:18 -0800 Subject: [PATCH] update websocket example --- examples/websocket/src/client.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/websocket/src/client.rs b/examples/websocket/src/client.rs index 45c3a712..06dad542 100644 --- a/examples/websocket/src/client.rs +++ b/examples/websocket/src/client.rs @@ -30,7 +30,7 @@ fn main() { }) .map(|(reader, writer)| { let addr: SyncAddress<_> = ChatClient::create(|ctx| { - ctx.add_stream(reader); + ChatClient::add_stream(reader, ctx); ChatClient(writer) }); @@ -68,7 +68,7 @@ impl Actor for ChatClient { } fn stopping(&mut self, _: &mut Context) -> bool { - println!("Disconnected"); + println!("Stopping"); // Stop application on disconnect Arbiter::system().send(actix::msgs::SystemExit(0)); @@ -96,13 +96,21 @@ impl Handler for ChatClient { } /// Handle server websocket messages -impl Handler> for ChatClient { - type Result = (); +impl StreamHandler for ChatClient { - fn handle(&mut self, msg: Result, ctx: &mut Context) { + fn handle(&mut self, msg: Message, ctx: &mut Context) { match msg { - Ok(Message::Text(txt)) => println!("Server: {:?}", txt), + Message::Text(txt) => println!("Server: {:?}", txt), _ => () } } + + fn started(&mut self, ctx: &mut Context, _: SpawnHandle) { + println!("Connected"); + } + + fn finished(&mut self, err: Option, ctx: &mut Context, _: SpawnHandle) { + println!("Server disconnected"); + ctx.stop() + } }