From 6ab76658680b7a1b1f9c4ed01b68b1c00a3140bc Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 17 Mar 2019 22:11:50 -0700 Subject: [PATCH] export ws module --- actix-web-actors/src/lib.rs | 7 +---- actix-web-actors/src/ws.rs | 17 ++++++------ actix-web-actors/tests/test_ws.rs | 44 ++++++++++++++++--------------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/actix-web-actors/src/lib.rs b/actix-web-actors/src/lib.rs index 0d4478654..5b64d7e0a 100644 --- a/actix-web-actors/src/lib.rs +++ b/actix-web-actors/src/lib.rs @@ -1,10 +1,5 @@ //! Actix actors integration for Actix web framework mod context; -mod ws; +pub mod ws; pub use self::context::HttpContext; -pub use self::ws::{ws_handshake, ws_start, WebsocketContext}; - -pub use actix_http::ws::CloseCode as WsCloseCode; -pub use actix_http::ws::ProtocolError as WsProtocolError; -pub use actix_http::ws::{Frame as WsFrame, Message as WsMessage}; diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index dca0f46df..b5f5c08c2 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -1,3 +1,4 @@ +//! Websocket integration use std::collections::VecDeque; use std::io; @@ -11,9 +12,11 @@ use actix::{ Message as ActixMessage, SpawnHandle, }; use actix_codec::{Decoder, Encoder}; -use actix_http::ws::{ - hash_key, CloseReason, Codec, Frame, HandshakeError, Message, ProtocolError, +use actix_http::ws::hash_key; +pub use actix_http::ws::{ + CloseCode, CloseReason, Codec, Frame, HandshakeError, Message, ProtocolError, }; + use actix_web::dev::{Head, HttpResponseBuilder}; use actix_web::error::{Error, ErrorInternalServerError, PayloadError}; use actix_web::http::{header, Method, StatusCode}; @@ -23,16 +26,12 @@ use futures::sync::oneshot::Sender; use futures::{Async, Future, Poll, Stream}; /// Do websocket handshake and start ws actor. -pub fn ws_start( - actor: A, - req: &HttpRequest, - stream: T, -) -> Result +pub fn start(actor: A, req: &HttpRequest, stream: T) -> Result where A: Actor> + StreamHandler, T: Stream + 'static, { - let mut res = ws_handshake(req)?; + let mut res = handshake(req)?; Ok(res.streaming(WebsocketContext::create(actor, stream))) } @@ -44,7 +43,7 @@ where // /// `protocols` is a sequence of known protocols. On successful handshake, // /// the returned response headers contain the first protocol in this list // /// which the server also knows. -pub fn ws_handshake(req: &HttpRequest) -> Result { +pub fn handshake(req: &HttpRequest) -> Result { // WebSocket accepts only GET if *req.method() != Method::GET { return Err(HandshakeError::GetMethodRequired); diff --git a/actix-web-actors/tests/test_ws.rs b/actix-web-actors/tests/test_ws.rs index 0a214644f..ea9c8d8fe 100644 --- a/actix-web-actors/tests/test_ws.rs +++ b/actix-web-actors/tests/test_ws.rs @@ -9,18 +9,18 @@ use futures::{Sink, Stream}; struct Ws; impl Actor for Ws { - type Context = WebsocketContext; + type Context = ws::WebsocketContext; } -impl StreamHandler for Ws { - fn handle(&mut self, msg: WsFrame, ctx: &mut Self::Context) { +impl StreamHandler for Ws { + fn handle(&mut self, msg: ws::Frame, ctx: &mut Self::Context) { match msg { - WsFrame::Ping(msg) => ctx.pong(&msg), - WsFrame::Text(text) => { + ws::Frame::Ping(msg) => ctx.pong(&msg), + ws::Frame::Text(text) => { ctx.text(String::from_utf8_lossy(&text.unwrap())).to_owned() } - WsFrame::Binary(bin) => ctx.binary(bin.unwrap()), - WsFrame::Close(reason) => ctx.close(reason), + ws::Frame::Binary(bin) => ctx.binary(bin.unwrap()), + ws::Frame::Close(reason) => ctx.close(reason), _ => (), } } @@ -28,40 +28,42 @@ impl StreamHandler for Ws { #[test] fn test_simple() { - let mut srv = - TestServer::new(|| { - HttpService::new(App::new().service(web::resource("/").to( - |req: HttpRequest, stream: web::Payload<_>| ws_start(Ws, &req, stream), - ))) - }); + let mut srv = TestServer::new(|| { + HttpService::new(App::new().service(web::resource("/").to( + |req: HttpRequest, stream: web::Payload<_>| ws::start(Ws, &req, stream), + ))) + }); // client service let framed = srv.ws().unwrap(); let framed = srv - .block_on(framed.send(WsMessage::Text("text".to_string()))) + .block_on(framed.send(ws::Message::Text("text".to_string()))) .unwrap(); let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap(); - assert_eq!(item, Some(WsFrame::Text(Some(BytesMut::from("text"))))); + assert_eq!(item, Some(ws::Frame::Text(Some(BytesMut::from("text"))))); let framed = srv - .block_on(framed.send(WsMessage::Binary("text".into()))) + .block_on(framed.send(ws::Message::Binary("text".into()))) .unwrap(); let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap(); assert_eq!( item, - Some(WsFrame::Binary(Some(Bytes::from_static(b"text").into()))) + Some(ws::Frame::Binary(Some(Bytes::from_static(b"text").into()))) ); let framed = srv - .block_on(framed.send(WsMessage::Ping("text".into()))) + .block_on(framed.send(ws::Message::Ping("text".into()))) .unwrap(); let (item, framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap(); - assert_eq!(item, Some(WsFrame::Pong("text".to_string().into()))); + assert_eq!(item, Some(ws::Frame::Pong("text".to_string().into()))); let framed = srv - .block_on(framed.send(WsMessage::Close(Some(WsCloseCode::Normal.into())))) + .block_on(framed.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))) .unwrap(); let (item, _framed) = srv.block_on(framed.into_future()).map_err(|_| ()).unwrap(); - assert_eq!(item, Some(WsFrame::Close(Some(WsCloseCode::Normal.into())))); + assert_eq!( + item, + Some(ws::Frame::Close(Some(ws::CloseCode::Normal.into()))) + ); }