diff --git a/examples/state.rs b/examples/state.rs index 5b8f6a4ca..620f67826 100644 --- a/examples/state.rs +++ b/examples/state.rs @@ -53,7 +53,7 @@ impl Handler for MyWebSocket { self.counter += 1; println!("WS({}): {:?}", self.counter, msg); match msg { - ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, 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::Closed | ws::Message::Error => { diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index 50ccfc779..1e436d166 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -82,7 +82,7 @@ impl Handler for WsChatSession { println!("WEBSOCKET MESSAGE: {:?}", msg); match msg { ws::Message::Ping(msg) => - ws::WsWriter::pong(ctx, msg), + ws::WsWriter::pong(ctx, &msg), ws::Message::Pong(msg) => self.hb = Instant::now(), ws::Message::Text(text) => { diff --git a/examples/websocket.rs b/examples/websocket.rs index 8d41de4da..b5f03f9cc 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -53,7 +53,7 @@ impl Handler for MyWebSocket { // process websocket messages println!("WS: {:?}", msg); match msg { - ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, 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::Closed | ws::Message::Error => { diff --git a/src/ws.rs b/src/ws.rs index a017f7a97..1319418f9 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -49,7 +49,7 @@ //! -> Response //! { //! match msg { -//! ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, 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), //! _ => (), @@ -77,6 +77,7 @@ use httpresponse::{ConnectionType, HttpResponse}; use wsframe; use wsproto::*; +pub use wsproto::CloseCode; #[doc(hidden)] const SEC_WEBSOCKET_ACCEPT: &'static str = "SEC-WEBSOCKET-ACCEPT"; @@ -303,11 +304,10 @@ impl WsWriter { } /// Send ping frame - pub fn ping(ctx: &mut HttpContext, message: String) + pub fn ping(ctx: &mut HttpContext, message: &str) where A: Actor> + Route { - let mut frame = wsframe::Frame::message( - Vec::from(message.as_str()), OpCode::Ping, true); + let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Ping, true); let mut buf = Vec::new(); frame.format(&mut buf).unwrap(); @@ -315,16 +315,25 @@ impl WsWriter { } /// Send pong frame - pub fn pong(ctx: &mut HttpContext, message: String) + pub fn pong(ctx: &mut HttpContext, message: &str) where A: Actor> + Route { - let mut frame = wsframe::Frame::message( - Vec::from(message.as_str()), OpCode::Pong, true); + let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Pong, true); let mut buf = Vec::new(); frame.format(&mut buf).unwrap(); ctx.write(buf); } + + /// Send close frame + pub fn close(ctx: &mut HttpContext, code: CloseCode, reason: &str) + where A: Actor> + Route + { + let mut frame = wsframe::Frame::close(code, reason); + let mut buf = Vec::new(); + frame.format(&mut buf).unwrap(); + ctx.write(buf); + } } #[cfg(test)]