diff --git a/src/lib.rs b/src/lib.rs index 5dd0309f..180e29af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,6 +175,7 @@ pub use httprequest::HttpRequest; pub use httpresponse::HttpResponse; pub use json::Json; pub use scope::Scope; +pub use ws::WsWriter; #[cfg(feature = "openssl")] pub(crate) const HAS_OPENSSL: bool = true; diff --git a/src/ws/client.rs b/src/ws/client.rs index 8a4abcae..07b44b4d 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -27,7 +27,7 @@ use client::{ClientConnector, ClientRequest, ClientRequestBuilder, ClientRespons use super::frame::Frame; use super::proto::{CloseReason, OpCode}; -use super::{Message, ProtocolError}; +use super::{Message, ProtocolError, WsWriter}; /// Websocket client error #[derive(Fail, Debug)] @@ -503,13 +503,6 @@ pub struct ClientWriter { inner: Rc>, } -impl ClientWriter { - #[inline] - fn as_mut(&mut self) -> &mut Inner { - unsafe { &mut *self.inner.get() } - } -} - impl ClientWriter { /// Write payload #[inline] @@ -521,21 +514,28 @@ impl ClientWriter { } } + #[inline] + fn as_mut(&mut self) -> &mut Inner { + unsafe { &mut *self.inner.get() } + } +} + +impl WsWriter for ClientWriter { /// Send text frame #[inline] - pub fn text>(&mut self, text: T) { + fn text>(&mut self, text: T) { self.write(Frame::message(text.into(), OpCode::Text, true, true)); } /// Send binary frame #[inline] - pub fn binary>(&mut self, data: B) { + fn binary>(&mut self, data: B) { self.write(Frame::message(data, OpCode::Binary, true, true)); } /// Send ping frame #[inline] - pub fn ping(&mut self, message: &str) { + fn ping(&mut self, message: &str) { self.write(Frame::message( Vec::from(message), OpCode::Ping, @@ -546,7 +546,7 @@ impl ClientWriter { /// Send pong frame #[inline] - pub fn pong(&mut self, message: &str) { + fn pong(&mut self, message: &str) { self.write(Frame::message( Vec::from(message), OpCode::Pong, @@ -557,7 +557,7 @@ impl ClientWriter { /// Send close frame #[inline] - pub fn close(&mut self, reason: Option) { + fn close(&mut self, reason: Option) { self.write(Frame::close(reason, true)); } } diff --git a/src/ws/context.rs b/src/ws/context.rs index b5a2456c..723b215a 100644 --- a/src/ws/context.rs +++ b/src/ws/context.rs @@ -13,6 +13,7 @@ use context::{ActorHttpContext, Drain, Frame as ContextFrame}; use error::{Error, ErrorInternalServerError}; use httprequest::HttpRequest; +use ws::WsWriter; use ws::frame::Frame; use ws::proto::{CloseReason, OpCode}; @@ -140,46 +141,6 @@ where &mut self.request } - /// Send text frame - #[inline] - pub fn text>(&mut self, text: T) { - self.write(Frame::message(text.into(), OpCode::Text, true, false)); - } - - /// Send binary frame - #[inline] - pub fn binary>(&mut self, data: B) { - self.write(Frame::message(data, OpCode::Binary, true, false)); - } - - /// Send ping frame - #[inline] - pub fn ping(&mut self, message: &str) { - self.write(Frame::message( - Vec::from(message), - OpCode::Ping, - true, - false, - )); - } - - /// Send pong frame - #[inline] - pub fn pong(&mut self, message: &str) { - self.write(Frame::message( - Vec::from(message), - OpCode::Pong, - true, - false, - )); - } - - /// Send close frame - #[inline] - pub fn close(&mut self, reason: Option) { - self.write(Frame::close(reason, false)); - } - /// Returns drain future pub fn drain(&mut self) -> Drain { let (tx, rx) = oneshot::channel(); @@ -213,6 +174,52 @@ where } } +impl WsWriter for WebsocketContext +where + A: Actor, + S: 'static, +{ + /// Send text frame + #[inline] + fn text>(&mut self, text: T) { + self.write(Frame::message(text.into(), OpCode::Text, true, false)); + } + + /// Send binary frame + #[inline] + fn binary>(&mut self, data: B) { + self.write(Frame::message(data, OpCode::Binary, true, false)); + } + + /// Send ping frame + #[inline] + fn ping(&mut self, message: &str) { + self.write(Frame::message( + Vec::from(message), + OpCode::Ping, + true, + false, + )); + } + + /// Send pong frame + #[inline] + fn pong(&mut self, message: &str) { + self.write(Frame::message( + Vec::from(message), + OpCode::Pong, + true, + false, + )); + } + + /// Send close frame + #[inline] + fn close(&mut self, reason: Option) { + self.write(Frame::close(reason, false)); + } +} + impl ActorHttpContext for WebsocketContext where A: Actor, diff --git a/src/ws/mod.rs b/src/ws/mod.rs index 402f2bdf..a39b95b1 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -340,6 +340,20 @@ where } } +/// Common writing methods for a websocket. +pub trait WsWriter { + /// Send a text + fn text>(&mut self, text: T); + /// Send a binary + fn binary>(&mut self, data: B); + /// Send a ping message + fn ping(&mut self, message: &str); + /// Send a pong message + fn pong(&mut self, message: &str); + /// Close the connection + fn close(&mut self, reason: Option); +} + #[cfg(test)] mod tests { use super::*;