Add `MaybeTlsStream` wrapper type for streams that optionally use TLS

Connecting to a URL wraps the connection into this wrapper type based
on the protocol of the URL (`ws:` or `wss:`). Making this type public
allows client applications to specify the websocket's type without
importing the tokio_tls crate.
pull/1/head
Andreas Neuhaus 6 years ago
parent e3174bf2a2
commit 430c06048d
  1. 11
      src/connect.rs
  2. 3
      src/lib.rs

@ -39,10 +39,12 @@ mod encryption {
use tungstenite::Error; use tungstenite::Error;
use tungstenite::stream::Mode; use tungstenite::stream::Mode;
use stream::NoDelay; use stream::{NoDelay, Stream as StreamSwitcher};
/// A stream that might be protected with TLS.
pub type MaybeTlsStream<S> = StreamSwitcher<S, TlsStream<S>>;
pub use stream::Stream as StreamSwitcher; pub type AutoStream<S> = MaybeTlsStream<S>;
pub type AutoStream<S> = StreamSwitcher<S, TlsStream<S>>;
impl<T: Read + Write + NoDelay> NoDelay for TlsStream<T> { impl<T: Read + Write + NoDelay> NoDelay for TlsStream<T> {
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> {
@ -68,6 +70,9 @@ mod encryption {
} }
} }
#[cfg(feature="tls")]
pub use self::encryption::MaybeTlsStream;
#[cfg(not(feature="tls"))] #[cfg(not(feature="tls"))]
mod encryption { mod encryption {
use futures::{future, Future}; use futures::{future, Future};

@ -45,6 +45,9 @@ use tungstenite::{
#[cfg(feature="connect")] #[cfg(feature="connect")]
pub use connect::{connect_async, client_async_tls}; pub use connect::{connect_async, client_async_tls};
#[cfg(all(feature="connect", feature="tls"))]
pub use connect::MaybeTlsStream;
/// Creates a WebSocket handshake from a request and a stream. /// Creates a WebSocket handshake from a request and a stream.
/// For convenience, the user may call this with a url string, a URL, /// For convenience, the user may call this with a url string, a URL,
/// or a `Request`. Calling with `Request` allows the user to add /// or a `Request`. Calling with `Request` allows the user to add

Loading…
Cancel
Save