Use Bytes for Ping/Pong on ws

This commit is contained in:
Heinz Gies 2019-09-10 12:21:07 +02:00 committed by Heinz N. Gies
parent fba31d4e0a
commit 73580fb527
3 changed files with 15 additions and 15 deletions

View File

@ -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<CloseReason>),
/// No-op. Useful for actix-net services
@ -30,9 +30,9 @@ pub enum Frame {
/// Binary frame
Binary(Option<BytesMut>),
/// Ping message
Ping(String),
Ping(Bytes),
/// Pong message
Pong(String),
Pong(Bytes),
/// Close message with optional reason
Close(Option<CloseReason>),
}
@ -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))),

View File

@ -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<B: Into<Bytes>>(&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<B: Into<Bytes>>(&mut self, message: B) {
self.write_raw(Message::Pong(message.into()));
}
/// Send close frame

View File

@ -15,7 +15,7 @@ impl Actor for Ws {
impl StreamHandler<ws::Message, ws::ProtocolError> 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),