From d85081b64e10e7bf76e785bfa2581b68cfaf1c3b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 10 Jan 2018 10:40:14 -0800 Subject: [PATCH] update websocket examples --- examples/websocket-chat/src/main.rs | 21 +++++++++------------ examples/websocket/src/main.rs | 10 +++++----- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index aec05ec74..8051e0a76 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -52,7 +52,7 @@ struct WsChatSession { } impl Actor for WsChatSession { - type Context = HttpContext; + type Context = ws::WebsocketContext; /// Method is called on actor start. /// We register ws session with ChatServer @@ -87,7 +87,7 @@ impl Handler for WsChatSession { type Result = (); fn handle(&mut self, msg: session::Message, ctx: &mut Self::Context) { - ws::WsWriter::text(ctx, &msg.0); + ctx.text(&msg.0); } } @@ -98,10 +98,8 @@ impl Handler for WsChatSession { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { println!("WEBSOCKET MESSAGE: {:?}", msg); match msg { - ws::Message::Ping(msg) => - ws::WsWriter::pong(ctx, &msg), - ws::Message::Pong(msg) => - self.hb = Instant::now(), + ws::Message::Ping(msg) => ctx.pong(&msg), + ws::Message::Pong(msg) => self.hb = Instant::now(), ws::Message::Text(text) => { let m = text.trim(); // we check for /sss type of messages @@ -115,7 +113,7 @@ impl Handler for WsChatSession { match res { Ok(Ok(rooms)) => { for room in rooms { - ws::WsWriter::text(ctx, &room); + ctx.text(&room); } }, _ => println!("Something is wrong"), @@ -132,20 +130,19 @@ impl Handler for WsChatSession { ctx.state().addr.send( server::Join{id: self.id, name: self.room.clone()}); - ws::WsWriter::text(ctx, "joined"); + ctx.text("joined"); } else { - ws::WsWriter::text(ctx, "!!! room name is required"); + ctx.text("!!! room name is required"); } }, "/name" => { if v.len() == 2 { self.name = Some(v[1].to_owned()); } else { - ws::WsWriter::text(ctx, "!!! name is required"); + ctx.text("!!! name is required"); } }, - _ => ws::WsWriter::text( - ctx, &format!("!!! unknown command: {:?}", m)), + _ => ctx.text(&format!("!!! unknown command: {:?}", m)), } } else { let msg = if let Some(ref name) = self.name { diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index fea8929de..a6abac908 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -21,20 +21,20 @@ fn ws_index(r: HttpRequest) -> Result { struct MyWebSocket; impl Actor for MyWebSocket { - type Context = HttpContext; + type Context = ws::WebsocketContext; } /// Handler for `ws::Message` impl Handler for MyWebSocket { type Result = (); - fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext) { + fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { // process websocket messages println!("WS: {:?}", msg); match msg { - ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg), - ws::Message::Text(text) => ws::WsWriter::text(ctx, &text), - ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), + ws::Message::Ping(msg) => ctx.pong(&msg), + ws::Message::Text(text) => ctx.text(&text), + ws::Message::Binary(bin) => ctx.binary(bin), ws::Message::Closed | ws::Message::Error => { ctx.stop(); }