From 73580fb52783fe6b6c0c9c3441119e9d1d7d3b07 Mon Sep 17 00:00:00 2001 From: Heinz Gies Date: Tue, 10 Sep 2019 12:21:07 +0200 Subject: [PATCH] Use Bytes for Ping/Pong on ws --- actix-http/src/ws/codec.rs | 20 ++++++++++---------- actix-web-actors/src/ws.rs | 8 ++++---- actix-web-actors/tests/test_ws.rs | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/actix-http/src/ws/codec.rs b/actix-http/src/ws/codec.rs index 9891bfa6e..f21e6ff1d 100644 --- a/actix-http/src/ws/codec.rs +++ b/actix-http/src/ws/codec.rs @@ -13,9 +13,9 @@ pub enum Message { /// Binary message Binary(Bytes), /// Ping message - Ping(String), + Ping(Bytes), /// Pong message - Pong(String), + Pong(Bytes), /// Close message with optional reason Close(Option), /// No-op. Useful for actix-net services @@ -30,9 +30,9 @@ pub enum Frame { /// Binary frame Binary(Option), /// Ping message - Ping(String), + Ping(Bytes), /// Pong message - Pong(String), + Pong(Bytes), /// Close message with optional reason Close(Option), } @@ -119,17 +119,17 @@ impl Decoder for Codec { } } OpCode::Ping => { - if let Some(ref pl) = payload { - Ok(Some(Frame::Ping(String::from_utf8_lossy(pl).into()))) + if let Some(pl) = payload { + Ok(Some(Frame::Ping(pl.into()))) } else { - Ok(Some(Frame::Ping(String::new()))) + Ok(Some(Frame::Ping(Bytes::new()))) } } OpCode::Pong => { - if let Some(ref pl) = payload { - Ok(Some(Frame::Pong(String::from_utf8_lossy(pl).into()))) + if let Some(pl) = payload { + Ok(Some(Frame::Pong(pl.into()))) } else { - Ok(Some(Frame::Pong(String::new()))) + Ok(Some(Frame::Pong(Bytes::new()))) } } OpCode::Binary => Ok(Some(Frame::Binary(payload))), diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index e25a7e6e4..b6b627962 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -345,14 +345,14 @@ where /// Send ping frame #[inline] - pub fn ping(&mut self, message: &str) { - self.write_raw(Message::Ping(message.to_string())); + pub fn ping>(&mut self, message: B) { + self.write_raw(Message::Ping(message.into())); } /// Send pong frame #[inline] - pub fn pong(&mut self, message: &str) { - self.write_raw(Message::Pong(message.to_string())); + pub fn pong>(&mut self, message: B) { + self.write_raw(Message::Pong(message.into())); } /// Send close frame diff --git a/actix-web-actors/tests/test_ws.rs b/actix-web-actors/tests/test_ws.rs index 687cf4314..49b69a1e9 100644 --- a/actix-web-actors/tests/test_ws.rs +++ b/actix-web-actors/tests/test_ws.rs @@ -15,7 +15,7 @@ impl Actor for Ws { impl StreamHandler for Ws { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { match msg { - ws::Message::Ping(msg) => ctx.pong(&msg), + ws::Message::Ping(msg) => ctx.pong(msg), ws::Message::Text(text) => ctx.text(text), ws::Message::Binary(bin) => ctx.binary(bin), ws::Message::Close(reason) => ctx.close(reason),