1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-13 02:39:32 +00: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;
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 => {

View file

@ -82,7 +82,7 @@ impl Handler<ws::Message> 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) => {

View file

@ -53,7 +53,7 @@ impl Handler<ws::Message> 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 => {

View file

@ -49,7 +49,7 @@
//! -> Response<Self, ws::Message>
//! {
//! 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<A>(ctx: &mut HttpContext<A>, message: String)
pub fn ping<A>(ctx: &mut HttpContext<A>, message: &str)
where A: Actor<Context=HttpContext<A>> + 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<A>(ctx: &mut HttpContext<A>, message: String)
pub fn pong<A>(ctx: &mut HttpContext<A>, message: &str)
where A: Actor<Context=HttpContext<A>> + 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<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)]