1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 05:41:50 +01:00

added WsWriter::close

This commit is contained in:
Nikolay Kim 2017-10-29 19:49:59 -07:00
parent 8ab04b39df
commit dec4140733
4 changed files with 19 additions and 10 deletions

View File

@ -53,7 +53,7 @@ impl Handler<ws::Message> for MyWebSocket {
self.counter += 1; self.counter += 1;
println!("WS({}): {:?}", self.counter, msg); println!("WS({}): {:?}", self.counter, msg);
match 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::Text(text) => ws::WsWriter::text(ctx, &text),
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
ws::Message::Closed | ws::Message::Error => { ws::Message::Closed | ws::Message::Error => {

View File

@ -82,7 +82,7 @@ impl Handler<ws::Message> for WsChatSession {
println!("WEBSOCKET MESSAGE: {:?}", msg); println!("WEBSOCKET MESSAGE: {:?}", msg);
match msg { match msg {
ws::Message::Ping(msg) => ws::Message::Ping(msg) =>
ws::WsWriter::pong(ctx, msg), ws::WsWriter::pong(ctx, &msg),
ws::Message::Pong(msg) => ws::Message::Pong(msg) =>
self.hb = Instant::now(), self.hb = Instant::now(),
ws::Message::Text(text) => { ws::Message::Text(text) => {

View File

@ -53,7 +53,7 @@ impl Handler<ws::Message> for MyWebSocket {
// 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) => ws::WsWriter::pong(ctx, &msg),
ws::Message::Text(text) => ws::WsWriter::text(ctx, &text), ws::Message::Text(text) => ws::WsWriter::text(ctx, &text),
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
ws::Message::Closed | ws::Message::Error => { ws::Message::Closed | ws::Message::Error => {

View File

@ -49,7 +49,7 @@
//! -> Response<Self, ws::Message> //! -> Response<Self, ws::Message>
//! { //! {
//! match 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::Text(text) => ws::WsWriter::text(ctx, &text),
//! ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin), //! ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
//! _ => (), //! _ => (),
@ -77,6 +77,7 @@ use httpresponse::{ConnectionType, HttpResponse};
use wsframe; use wsframe;
use wsproto::*; use wsproto::*;
pub use wsproto::CloseCode;
#[doc(hidden)] #[doc(hidden)]
const SEC_WEBSOCKET_ACCEPT: &'static str = "SEC-WEBSOCKET-ACCEPT"; const SEC_WEBSOCKET_ACCEPT: &'static str = "SEC-WEBSOCKET-ACCEPT";
@ -303,11 +304,10 @@ impl WsWriter {
} }
/// Send ping frame /// Send ping frame
pub fn ping<A>(ctx: &mut HttpContext<A>, message: String) pub fn ping<A>(ctx: &mut HttpContext<A>, message: &str)
where A: Actor<Context=HttpContext<A>> + Route where A: Actor<Context=HttpContext<A>> + Route
{ {
let mut frame = wsframe::Frame::message( let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Ping, true);
Vec::from(message.as_str()), OpCode::Ping, true);
let mut buf = Vec::new(); let mut buf = Vec::new();
frame.format(&mut buf).unwrap(); frame.format(&mut buf).unwrap();
@ -315,16 +315,25 @@ impl WsWriter {
} }
/// Send pong frame /// Send pong frame
pub fn pong<A>(ctx: &mut HttpContext<A>, message: String) pub fn pong<A>(ctx: &mut HttpContext<A>, message: &str)
where A: Actor<Context=HttpContext<A>> + Route where A: Actor<Context=HttpContext<A>> + Route
{ {
let mut frame = wsframe::Frame::message( let mut frame = wsframe::Frame::message(Vec::from(message), OpCode::Pong, true);
Vec::from(message.as_str()), OpCode::Pong, true);
let mut buf = Vec::new(); let mut buf = Vec::new();
frame.format(&mut buf).unwrap(); frame.format(&mut buf).unwrap();
ctx.write(buf); ctx.write(buf);
} }
/// Send close frame
pub fn close<A>(ctx: &mut HttpContext<A>, code: CloseCode, reason: &str)
where A: Actor<Context=HttpContext<A>> + Route
{
let mut frame = wsframe::Frame::close(code, reason);
let mut buf = Vec::new();
frame.format(&mut buf).unwrap();
ctx.write(buf);
}
} }
#[cfg(test)] #[cfg(test)]