@@ -15,7 +15,7 @@ use ws::receiver::Receiver as ReceiverTrait;
1515use result:: WebSocketResult ;
1616use stream:: { AsTcpStream , Stream , Splittable , Shutdown } ;
1717use dataframe:: DataFrame ;
18- use header:: { WebSocketProtocol , WebSocketExtensions , Origin } ;
18+ use header:: { WebSocketProtocol , WebSocketExtensions } ;
1919use header:: extensions:: Extension ;
2020
2121use ws:: dataframe:: DataFrame as DataFrameable ;
@@ -29,27 +29,28 @@ pub use self::builder::{ClientBuilder, Url, ParseError};
2929
3030/// Represents a WebSocket client, which can send and receive messages/data frames.
3131///
32- /// `D` is the data frame type, `S` is the type implementing `Sender<D>` and `R`
33- /// is the type implementing `Receiver<D>`.
32+ /// The client just wraps around a `Stream` (which is something that can be read from
33+ /// and written to) and handles the websocket protocol. TCP or SSL over TCP is common,
34+ /// but any stream can be used.
3435///
35- /// For most cases, the data frame type will be `dataframe::DataFrame`, the Sender
36- /// type will be `client::Sender<stream::WebSocketStream>` and the receiver type
37- /// will be `client::Receiver<stream::WebSocketStream>`.
38- ///
39- /// A `Client` can be split into a `Sender` and a `Receiver` which can then be moved
36+ /// A `Client` can also be split into a `Reader` and a `Writer` which can then be moved
4037/// to different threads, often using a send loop and receiver loop concurrently,
4138/// as shown in the client example in `examples/client.rs`.
39+ /// This is only possible for streams that implement the `Splittable` trait, which
40+ /// currently is only TCP streams. (it is unsafe to duplicate an SSL stream)
4241///
43- ///#Connecting to a Server
42+ ///# Connecting to a Server
4443///
4544///```no_run
4645///extern crate websocket;
4746///# fn main() {
4847///
4948///use websocket::{ClientBuilder, Message};
5049///
51- ///let mut client = ClientBuilder::new("ws://127.0.0.1:1234").unwrap()
52- /// .connect(None).unwrap();
50+ ///let mut client = ClientBuilder::new("ws://127.0.0.1:1234")
51+ /// .unwrap()
52+ /// .connect_insecure()
53+ /// .unwrap();
5354///
5455///let message = Message::text("Hello, World!");
5556///client.send_message(&message).unwrap(); // Send message
@@ -87,17 +88,20 @@ impl<S> Client<S>
8788 self . stream . get_ref ( ) . as_tcp ( ) . shutdown ( Shutdown :: Both )
8889 }
8990
90- /// See `TcpStream.peer_addr()`.
91+ /// See [`TcpStream::peer_addr`]
92+ /// (https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.peer_addr).
9193 pub fn peer_addr ( & self ) -> IoResult < SocketAddr > {
9294 self . stream . get_ref ( ) . as_tcp ( ) . peer_addr ( )
9395 }
9496
95- /// See `TcpStream.local_addr()`.
97+ /// See [`TcpStream::local_addr`]
98+ /// (https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.local_addr).
9699 pub fn local_addr ( & self ) -> IoResult < SocketAddr > {
97100 self . stream . get_ref ( ) . as_tcp ( ) . local_addr ( )
98101 }
99102
100- /// See `TcpStream.set_nodelay()`.
103+ /// See [`TcpStream::set_nodelay`]
104+ /// (https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.set_nodelay).
101105 pub fn set_nodelay ( & mut self , nodelay : bool ) -> IoResult < ( ) > {
102106 self . stream . get_ref ( ) . as_tcp ( ) . set_nodelay ( nodelay)
103107 }
@@ -115,6 +119,7 @@ impl<S> Client<S>
115119 /// **without sending any handshake** this is meant to only be used with
116120 /// a stream that has a websocket connection already set up.
117121 /// If in doubt, don't use this!
122+ #[ doc( hidden) ]
118123 pub fn unchecked ( stream : BufReader < S > , headers : Headers ) -> Self {
119124 Client {
120125 headers : headers,
@@ -159,40 +164,122 @@ impl<S> Client<S>
159164 self . receiver . recv_message ( & mut self . stream )
160165 }
161166
167+ /// Access the headers that were sent in the server's handshake response.
168+ /// This is a catch all for headers other than protocols and extensions.
162169 pub fn headers ( & self ) -> & Headers {
163170 & self . headers
164171 }
165172
173+ /// **If you supplied a protocol, you must check that it was accepted by
174+ /// the server** using this function.
175+ /// This is not done automatically because the terms of accepting a protocol
176+ /// can get complicated, especially if some protocols depend on others, etc.
177+ ///
178+ /// ```rust,no_run
179+ /// # use websocket::ClientBuilder;
180+ /// let mut client = ClientBuilder::new("wss://test.fysh.in").unwrap()
181+ /// .add_protocol("xmpp")
182+ /// .connect_insecure()
183+ /// .unwrap();
184+ ///
185+ /// // be sure to check the protocol is there!
186+ /// assert!(client.protocols().iter().any(|p| p as &str == "xmpp"));
187+ /// ```
166188 pub fn protocols ( & self ) -> & [ String ] {
167189 self . headers
168190 . get :: < WebSocketProtocol > ( )
169191 . map ( |p| p. 0 . as_slice ( ) )
170192 . unwrap_or ( & [ ] )
171193 }
172194
195+ /// If you supplied a protocol, be sure to check if it was accepted by the
196+ /// server here. Since no extensions are implemented out of the box yet, using
197+ /// one will require its own implementation.
173198 pub fn extensions ( & self ) -> & [ Extension ] {
174199 self . headers
175200 . get :: < WebSocketExtensions > ( )
176201 . map ( |e| e. 0 . as_slice ( ) )
177202 . unwrap_or ( & [ ] )
178203 }
179204
180- pub fn origin ( & self ) -> Option < & str > {
181- self . headers . get :: < Origin > ( ) . map ( |o| & o. 0 as & str )
182- }
183-
205+ /// Get a reference to the stream.
206+ /// Useful to be able to set options on the stream.
207+ ///
208+ /// ```rust,no_run
209+ /// # use websocket::ClientBuilder;
210+ /// let mut client = ClientBuilder::new("ws://double.down").unwrap()
211+ /// .connect_insecure()
212+ /// .unwrap();
213+ ///
214+ /// client.stream_ref().set_ttl(60).unwrap();
215+ /// ```
184216 pub fn stream_ref ( & self ) -> & S {
185217 self . stream . get_ref ( )
186218 }
187219
220+ /// Get a handle to the writable portion of this stream.
221+ /// This can be used to write custom extensions.
222+ ///
223+ /// ```rust,no_run
224+ /// # use websocket::ClientBuilder;
225+ /// use websocket::Message;
226+ /// use websocket::ws::sender::Sender as SenderTrait;
227+ /// use websocket::sender::Sender;
228+ ///
229+ /// let mut client = ClientBuilder::new("ws://the.room").unwrap()
230+ /// .connect_insecure()
231+ /// .unwrap();
232+ ///
233+ /// let message = Message::text("Oh hi, Mark.");
234+ /// let mut sender = Sender::new(true);
235+ /// let mut buf = Vec::new();
236+ ///
237+ /// sender.send_message(&mut buf, &message);
238+ ///
239+ /// /* transform buf somehow */
240+ ///
241+ /// client.writer_mut().write_all(&buf);
242+ /// ```
188243 pub fn writer_mut ( & mut self ) -> & mut Write {
189244 self . stream . get_mut ( )
190245 }
191246
247+ /// Get a handle to the readable portion of this stream.
248+ /// This can be used to transform raw bytes before they
249+ /// are read in.
250+ ///
251+ /// ```rust,no_run
252+ /// # use websocket::ClientBuilder;
253+ /// use std::io::Cursor;
254+ /// use websocket::Message;
255+ /// use websocket::ws::receiver::Receiver as ReceiverTrait;
256+ /// use websocket::receiver::Receiver;
257+ ///
258+ /// let mut client = ClientBuilder::new("ws://the.room").unwrap()
259+ /// .connect_insecure()
260+ /// .unwrap();
261+ ///
262+ /// let mut receiver = Receiver::new(false);
263+ /// let mut buf = Vec::new();
264+ ///
265+ /// client.reader_mut().read_to_end(&mut buf);
266+ ///
267+ /// /* transform buf somehow */
268+ ///
269+ /// let mut buf_reader = Cursor::new(&mut buf);
270+ /// let message: Message = receiver.recv_message(&mut buf_reader).unwrap();
271+ /// ```
192272 pub fn reader_mut ( & mut self ) -> & mut Read {
193273 & mut self . stream
194274 }
195275
276+ /// Deconstruct the client into its underlying stream and
277+ /// maybe some of the buffer that was already read from the stream.
278+ /// The client uses a buffered reader to read in messages, so some
279+ /// bytes might already be read from the stream when this is called,
280+ /// these buffered bytes are returned in the form
281+ ///
282+ /// `(byte_buffer: Vec<u8>, buffer_capacity: usize, buffer_position: usize)`
196283 pub fn into_stream ( self ) -> ( S , Option < ( Vec < u8 > , usize , usize ) > ) {
197284 let ( stream, buf, pos, cap) = self . stream . into_parts ( ) ;
198285 ( stream, Some ( ( buf, pos, cap) ) )
0 commit comments