mirror of
https://github.com/actix/actix-web.git
synced 2024-11-14 04:41:15 +00:00
added WsWriter::close
This commit is contained in:
parent
8ab04b39df
commit
dec4140733
4 changed files with 19 additions and 10 deletions
|
@ -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 => {
|
||||
|
|
|
@ -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) => {
|
||||
|
|
|
@ -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 => {
|
||||
|
|
23
src/ws.rs
23
src/ws.rs
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue