1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

update websocket examples

This commit is contained in:
Nikolay Kim 2018-01-10 10:40:14 -08:00
parent 4b72a1b325
commit d85081b64e
2 changed files with 14 additions and 17 deletions

View File

@ -52,7 +52,7 @@ struct WsChatSession {
} }
impl Actor for WsChatSession { impl Actor for WsChatSession {
type Context = HttpContext<Self, WsChatSessionState>; type Context = ws::WebsocketContext<Self, WsChatSessionState>;
/// Method is called on actor start. /// Method is called on actor start.
/// We register ws session with ChatServer /// We register ws session with ChatServer
@ -87,7 +87,7 @@ impl Handler<session::Message> for WsChatSession {
type Result = (); type Result = ();
fn handle(&mut self, msg: session::Message, ctx: &mut Self::Context) { 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<ws::Message> for WsChatSession {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
println!("WEBSOCKET MESSAGE: {:?}", msg); println!("WEBSOCKET MESSAGE: {:?}", msg);
match msg { match msg {
ws::Message::Ping(msg) => ws::Message::Ping(msg) => ctx.pong(&msg),
ws::WsWriter::pong(ctx, &msg), ws::Message::Pong(msg) => self.hb = Instant::now(),
ws::Message::Pong(msg) =>
self.hb = Instant::now(),
ws::Message::Text(text) => { ws::Message::Text(text) => {
let m = text.trim(); let m = text.trim();
// we check for /sss type of messages // we check for /sss type of messages
@ -115,7 +113,7 @@ impl Handler<ws::Message> for WsChatSession {
match res { match res {
Ok(Ok(rooms)) => { Ok(Ok(rooms)) => {
for room in rooms { for room in rooms {
ws::WsWriter::text(ctx, &room); ctx.text(&room);
} }
}, },
_ => println!("Something is wrong"), _ => println!("Something is wrong"),
@ -132,20 +130,19 @@ impl Handler<ws::Message> for WsChatSession {
ctx.state().addr.send( ctx.state().addr.send(
server::Join{id: self.id, name: self.room.clone()}); server::Join{id: self.id, name: self.room.clone()});
ws::WsWriter::text(ctx, "joined"); ctx.text("joined");
} else { } else {
ws::WsWriter::text(ctx, "!!! room name is required"); ctx.text("!!! room name is required");
} }
}, },
"/name" => { "/name" => {
if v.len() == 2 { if v.len() == 2 {
self.name = Some(v[1].to_owned()); self.name = Some(v[1].to_owned());
} else { } else {
ws::WsWriter::text(ctx, "!!! name is required"); ctx.text("!!! name is required");
} }
}, },
_ => ws::WsWriter::text( _ => ctx.text(&format!("!!! unknown command: {:?}", m)),
ctx, &format!("!!! unknown command: {:?}", m)),
} }
} else { } else {
let msg = if let Some(ref name) = self.name { let msg = if let Some(ref name) = self.name {

View File

@ -21,20 +21,20 @@ fn ws_index(r: HttpRequest) -> Result<HttpResponse> {
struct MyWebSocket; struct MyWebSocket;
impl Actor for MyWebSocket { impl Actor for MyWebSocket {
type Context = HttpContext<Self>; type Context = ws::WebsocketContext<Self>;
} }
/// Handler for `ws::Message` /// Handler for `ws::Message`
impl Handler<ws::Message> for MyWebSocket { impl Handler<ws::Message> for MyWebSocket {
type Result = (); type Result = ();
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
// process websocket messages // process websocket messages
println!("WS: {:?}", msg); println!("WS: {:?}", msg);
match msg { match msg {
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg), ws::Message::Ping(msg) => ctx.pong(&msg),
ws::Message::Text(text) => ws::WsWriter::text(ctx, &text), ws::Message::Text(text) => ctx.text(&text),
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), ws::Message::Binary(bin) => ctx.binary(bin),
ws::Message::Closed | ws::Message::Error => { ws::Message::Closed | ws::Message::Error => {
ctx.stop(); ctx.stop();
} }