1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-10 10:59:33 +00:00

Add WsWriter trait

`WsWriter` trait is a common interface for writing to a websocket and
it's implemented for both: `WebScoketContext` and `ClientWriter`.
This commit is contained in:
Denis Kolodin 2018-05-02 08:25:31 +03:00
parent a1958deaae
commit 80f385e703
4 changed files with 75 additions and 53 deletions

View file

@ -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;

View file

@ -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<UnsafeCell<Inner>>,
}
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<T: Into<Binary>>(&mut self, text: T) {
fn text<T: Into<Binary>>(&mut self, text: T) {
self.write(Frame::message(text.into(), OpCode::Text, true, true));
}
/// Send binary frame
#[inline]
pub fn binary<B: Into<Binary>>(&mut self, data: B) {
fn binary<B: Into<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<CloseReason>) {
fn close(&mut self, reason: Option<CloseReason>) {
self.write(Frame::close(reason, true));
}
}

View file

@ -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<T: Into<Binary>>(&mut self, text: T) {
self.write(Frame::message(text.into(), OpCode::Text, true, false));
}
/// Send binary frame
#[inline]
pub fn binary<B: Into<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<CloseReason>) {
self.write(Frame::close(reason, false));
}
/// Returns drain future
pub fn drain(&mut self) -> Drain<A> {
let (tx, rx) = oneshot::channel();
@ -213,6 +174,52 @@ where
}
}
impl<A, S> WsWriter for WebsocketContext<A, S>
where
A: Actor<Context = Self>,
S: 'static,
{
/// Send text frame
#[inline]
fn text<T: Into<Binary>>(&mut self, text: T) {
self.write(Frame::message(text.into(), OpCode::Text, true, false));
}
/// Send binary frame
#[inline]
fn binary<B: Into<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<CloseReason>) {
self.write(Frame::close(reason, false));
}
}
impl<A, S> ActorHttpContext for WebsocketContext<A, S>
where
A: Actor<Context = Self>,

View file

@ -340,6 +340,20 @@ where
}
}
/// Common writing methods for a websocket.
pub trait WsWriter {
/// Send a text
fn text<T: Into<Binary>>(&mut self, text: T);
/// Send a binary
fn binary<B: Into<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<CloseReason>);
}
#[cfg(test)]
mod tests {
use super::*;