mirror of https://github.com/fafhrd91/actix-web
Use Bytes for Ping/Pong on ws
This commit is contained in:
parent
fba31d4e0a
commit
73580fb527
|
@ -13,9 +13,9 @@ pub enum Message {
|
||||||
/// Binary message
|
/// Binary message
|
||||||
Binary(Bytes),
|
Binary(Bytes),
|
||||||
/// Ping message
|
/// Ping message
|
||||||
Ping(String),
|
Ping(Bytes),
|
||||||
/// Pong message
|
/// Pong message
|
||||||
Pong(String),
|
Pong(Bytes),
|
||||||
/// Close message with optional reason
|
/// Close message with optional reason
|
||||||
Close(Option<CloseReason>),
|
Close(Option<CloseReason>),
|
||||||
/// No-op. Useful for actix-net services
|
/// No-op. Useful for actix-net services
|
||||||
|
@ -30,9 +30,9 @@ pub enum Frame {
|
||||||
/// Binary frame
|
/// Binary frame
|
||||||
Binary(Option<BytesMut>),
|
Binary(Option<BytesMut>),
|
||||||
/// Ping message
|
/// Ping message
|
||||||
Ping(String),
|
Ping(Bytes),
|
||||||
/// Pong message
|
/// Pong message
|
||||||
Pong(String),
|
Pong(Bytes),
|
||||||
/// Close message with optional reason
|
/// Close message with optional reason
|
||||||
Close(Option<CloseReason>),
|
Close(Option<CloseReason>),
|
||||||
}
|
}
|
||||||
|
@ -119,17 +119,17 @@ impl Decoder for Codec {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::Ping => {
|
OpCode::Ping => {
|
||||||
if let Some(ref pl) = payload {
|
if let Some(pl) = payload {
|
||||||
Ok(Some(Frame::Ping(String::from_utf8_lossy(pl).into())))
|
Ok(Some(Frame::Ping(pl.into())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Some(Frame::Ping(String::new())))
|
Ok(Some(Frame::Ping(Bytes::new())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::Pong => {
|
OpCode::Pong => {
|
||||||
if let Some(ref pl) = payload {
|
if let Some(pl) = payload {
|
||||||
Ok(Some(Frame::Pong(String::from_utf8_lossy(pl).into())))
|
Ok(Some(Frame::Pong(pl.into())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Some(Frame::Pong(String::new())))
|
Ok(Some(Frame::Pong(Bytes::new())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpCode::Binary => Ok(Some(Frame::Binary(payload))),
|
OpCode::Binary => Ok(Some(Frame::Binary(payload))),
|
||||||
|
|
|
@ -345,14 +345,14 @@ where
|
||||||
|
|
||||||
/// Send ping frame
|
/// Send ping frame
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn ping(&mut self, message: &str) {
|
pub fn ping<B: Into<Bytes>>(&mut self, message: B) {
|
||||||
self.write_raw(Message::Ping(message.to_string()));
|
self.write_raw(Message::Ping(message.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send pong frame
|
/// Send pong frame
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn pong(&mut self, message: &str) {
|
pub fn pong<B: Into<Bytes>>(&mut self, message: B) {
|
||||||
self.write_raw(Message::Pong(message.to_string()));
|
self.write_raw(Message::Pong(message.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send close frame
|
/// Send close frame
|
||||||
|
|
|
@ -15,7 +15,7 @@ impl Actor for Ws {
|
||||||
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
|
||||||
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
|
||||||
match msg {
|
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::Text(text) => ctx.text(text),
|
||||||
ws::Message::Binary(bin) => ctx.binary(bin),
|
ws::Message::Binary(bin) => ctx.binary(bin),
|
||||||
ws::Message::Close(reason) => ctx.close(reason),
|
ws::Message::Close(reason) => ctx.close(reason),
|
||||||
|
|
Loading…
Reference in New Issue