Skip to content

Commit bc8a0a4

Browse files
committed
Updated doc examples and tests, added to travis.
1 parent 7000b3a commit bc8a0a4

File tree

10 files changed

+72
-64
lines changed

10 files changed

+72
-64
lines changed

examples/async-autobahn-server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,3 @@ fn main() {
5252

5353
core.run(f).unwrap();
5454
}
55-

scripts/autobahn-client.sh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,25 @@ wstest -m fuzzingserver -s 'autobahn/fuzzingserver.json' & \
1515
FUZZINGSERVER_PID=$!
1616
sleep 10
1717

18+
function test_diff() {
19+
DIFF=$(diff \
20+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client-results.json') \
21+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client/index.json') )
22+
23+
if [[ $DIFF ]]; then
24+
echo 'Difference in results, either this is a regression or' \
25+
'one should update autobahn/client-results.json with the new results.' \
26+
'The results are:'
27+
echo $DIFF
28+
exit 64
29+
fi
30+
}
31+
1832
cargo build --example autobahn-client
1933
cargo run --example autobahn-client
34+
test_diff
2035

21-
DIFF=$(diff \
22-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client-results.json') \
23-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client/index.json') )
24-
25-
if [[ $DIFF ]]; then
26-
echo Difference in results, either this is a regression or \
27-
one should update autobahn/client-results.json with the new results. \
28-
The results are:
29-
echo $DIFF
30-
exit 64
31-
fi
36+
cargo build --example async-autobahn-client
37+
cargo run --example async-autobahn-client
38+
test_diff
3239

scripts/autobahn-server.sh

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,35 @@ function cleanup() {
1212
}
1313
trap cleanup TERM EXIT
1414

15+
function test_diff() {
16+
DIFF=$(diff \
17+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server-results.json') \
18+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server/index.json') )
19+
20+
if [[ $DIFF ]]; then
21+
echo Difference in results, either this is a regression or \
22+
one should update autobahn/server-results.json with the new results. \
23+
The results are:
24+
echo $DIFF
25+
exit 64
26+
fi
27+
}
28+
29+
# Test synchronous version
1530
cargo build --example autobahn-server
16-
./target/debug/examples/autobahn-server & \
17-
WSSERVER_PID=$!
31+
./target/debug/examples/autobahn-server & WSSERVER_PID=$!
1832
echo "Server PID: ${WSSERVER_PID}"
1933
sleep 10
20-
2134
wstest -m fuzzingclient -s 'autobahn/fuzzingclient.json'
35+
kill -9 ${WSSERVER_PID}
36+
test_diff
2237

23-
DIFF=$(diff \
24-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server-results.json') \
25-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server/index.json') )
26-
27-
if [[ $DIFF ]]; then
28-
echo Difference in results, either this is a regression or \
29-
one should update autobahn/server-results.json with the new results. \
30-
The results are:
31-
echo $DIFF
32-
exit 64
33-
fi
38+
# Test asynchronous version
39+
cargo build --example async-autobahn-server
40+
./target/debug/examples/async-autobahn-server & WSSERVER_PID=$!
41+
echo "Server PID: ${WSSERVER_PID}"
42+
sleep 10
43+
wstest -m fuzzingclient -s 'autobahn/fuzzingclient.json'
44+
kill -9 ${WSSERVER_PID}
45+
test_diff
3446

src/client/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'u> ClientBuilder<'u> {
443443
///
444444
/// ```rust
445445
/// # use websocket::ClientBuilder;
446-
/// use websocket::stream::ReadWritePair;
446+
/// use websocket::sync::stream::ReadWritePair;
447447
/// use std::io::Cursor;
448448
///
449449
/// let accept = b"HTTP/1.1 101 Switching Protocols\r

src/client/sync.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,9 @@ impl<S> Client<S>
165165
///
166166
/// client.send_message(&Message::text("Hello world!")).unwrap();
167167
///
168-
/// let message: Message = client.recv_message().unwrap();
168+
/// let response = client.recv_message().unwrap();
169169
/// ```
170-
pub fn recv_message<I>(&mut self) -> WebSocketResult<OwnedMessage>
171-
where I: Iterator<Item = DataFrame>
172-
{
170+
pub fn recv_message(&mut self) -> WebSocketResult<OwnedMessage> {
173171
self.receiver.recv_message(&mut self.stream)
174172
}
175173

@@ -260,7 +258,6 @@ impl<S> Client<S>
260258
/// ```rust,no_run
261259
/// # use websocket::ClientBuilder;
262260
/// use std::io::Cursor;
263-
/// use websocket::Message;
264261
/// use websocket::ws::receiver::Receiver as ReceiverTrait;
265262
/// use websocket::receiver::Receiver;
266263
///
@@ -276,7 +273,7 @@ impl<S> Client<S>
276273
/// /* transform buf somehow */
277274
///
278275
/// let mut buf_reader = Cursor::new(&mut buf);
279-
/// let message: Message = receiver.recv_message(&mut buf_reader).unwrap();
276+
/// let message = receiver.recv_message(&mut buf_reader).unwrap();
280277
/// ```
281278
pub fn reader_mut(&mut self) -> &mut Read {
282279
&mut self.stream
@@ -299,14 +296,13 @@ impl<S> Client<S>
299296
///```no_run
300297
///# extern crate websocket;
301298
///# fn main() {
302-
///use websocket::{ClientBuilder, Message};
299+
///use websocket::ClientBuilder;
303300
///
304301
///let mut client = ClientBuilder::new("ws://127.0.0.1:1234").unwrap()
305302
/// .connect(None).unwrap();
306303
///
307304
///for message in client.incoming_messages() {
308-
/// let message: Message = message.unwrap();
309-
/// println!("Recv: {:?}", message);
305+
/// println!("Recv: {:?}", message.unwrap());
310306
///}
311307
///# }
312308
///```
@@ -318,17 +314,16 @@ impl<S> Client<S>
318314
///```no_run
319315
///# extern crate websocket;
320316
///# fn main() {
321-
///use websocket::{ClientBuilder, Message};
317+
///use websocket::ClientBuilder;
322318
///
323319
///let mut client = ClientBuilder::new("ws://127.0.0.1:1234").unwrap()
324320
/// .connect_insecure().unwrap();
325321
///
326322
///let (mut receiver, mut sender) = client.split().unwrap();
327323
///
328324
///for message in receiver.incoming_messages() {
329-
/// let message: Message = message.unwrap();
330325
/// // Echo the message back
331-
/// sender.send_message(&message).unwrap();
326+
/// sender.send_message(&message.unwrap()).unwrap();
332327
///}
333328
///# }
334329
///```
@@ -357,8 +352,7 @@ impl<S> Client<S>
357352
///
358353
///thread::spawn(move || {
359354
/// for message in receiver.incoming_messages() {
360-
/// let message: Message = message.unwrap();
361-
/// println!("Recv: {:?}", message);
355+
/// println!("Recv: {:?}", message.unwrap());
362356
/// }
363357
///});
364358
///

src/result.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use native_tls::HandshakeError as TlsHandshakeError;
1717
/// The type used for WebSocket results
1818
pub type WebSocketResult<T> = Result<T, WebSocketError>;
1919

20+
#[cfg(feature="async")]
2021
pub mod async {
2122
use futures::Future;
2223
use super::WebSocketError;

src/server/sync.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::io;
44
use std::convert::Into;
55
#[cfg(feature="sync-ssl")]
66
use native_tls::{TlsStream, TlsAcceptor};
7-
use stream::Stream;
87
use server::{WsServer, OptionalTlsAcceptor, NoTlsAcceptor, InvalidConnection};
98
use server::upgrade::sync::{Upgrade, IntoWs, Buffer};
109
pub use server::upgrade::{Request, HyperIntoWsError};
@@ -33,7 +32,8 @@ pub type AcceptResult<S> = Result<Upgrade<S>, InvalidConnection<S, Buffer>>;
3332
///extern crate websocket;
3433
///# fn main() {
3534
///use std::thread;
36-
///use websocket::{Server, Message};
35+
///use websocket::Message;
36+
///use websocket::sync::Server;
3737
///
3838
///let server = Server::bind("127.0.0.1:1234").unwrap();
3939
///
@@ -54,30 +54,24 @@ pub type AcceptResult<S> = Result<Upgrade<S>, InvalidConnection<S, Buffer>>;
5454
///# Secure Servers
5555
/// ```no_run
5656
///extern crate websocket;
57-
///extern crate openssl;
57+
///extern crate native_tls;
5858
///# fn main() {
5959
///use std::thread;
6060
///use std::io::Read;
6161
///use std::fs::File;
62-
///use websocket::{Server, Message};
63-
///use openssl::pkcs12::Pkcs12;
64-
///use openssl::ssl::{SslMethod, SslAcceptorBuilder, SslStream};
62+
///use websocket::Message;
63+
///use websocket::sync::Server;
64+
///use native_tls::{Pkcs12, TlsAcceptor};
6565
///
6666
///// In this example we retrieve our keypair and certificate chain from a PKCS #12 archive,
6767
///// but but they can also be retrieved from, for example, individual PEM- or DER-formatted
6868
///// files. See the documentation for the `PKey` and `X509` types for more details.
6969
///let mut file = File::open("identity.pfx").unwrap();
7070
///let mut pkcs12 = vec![];
7171
///file.read_to_end(&mut pkcs12).unwrap();
72-
///let pkcs12 = Pkcs12::from_der(&pkcs12).unwrap();
73-
///let identity = pkcs12.parse("password123").unwrap();
72+
///let pkcs12 = Pkcs12::from_der(&pkcs12, "hacktheplanet").unwrap();
7473
///
75-
///let acceptor = SslAcceptorBuilder::mozilla_intermediate(SslMethod::tls(),
76-
/// &identity.pkey,
77-
/// &identity.cert,
78-
/// &identity.chain)
79-
/// .unwrap()
80-
/// .build();
74+
///let acceptor = TlsAcceptor::builder(pkcs12).unwrap().build().unwrap();
8175
///
8276
///let server = Server::bind_secure("127.0.0.1:1234", acceptor).unwrap();
8377
///
@@ -116,14 +110,16 @@ impl<S> WsServer<S, TcpListener>
116110
}
117111

118112
/// Changes whether the Server is in nonblocking mode.
113+
/// NOTE: It is strongly encouraged to use the `websocket::async` module instead
114+
/// of this. It provides high level APIs for creating asynchronous servers.
119115
///
120-
/// If it is in nonblocking mode, accept() will return an error instead of blocking when there
121-
/// are no incoming connections.
116+
/// If it is in nonblocking mode, accept() will return an error instead of
117+
/// blocking when there are no incoming connections.
122118
///
123119
///# Examples
124120
///```no_run
125121
/// # extern crate websocket;
126-
/// # use websocket::Server;
122+
/// # use websocket::sync::Server;
127123
/// # fn main() {
128124
/// // Suppose we have to work in a single thread, but want to
129125
/// // accomplish two unrelated things:

src/server/upgrade/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Allows you to take an existing request or stream of data and convert it into a
22
//! WebSocket client.
33
use std::error::Error;
4-
use std::net::TcpStream;
54
use std::io;
65
use std::fmt::{self, Formatter, Display};
76
use stream::Stream;

src/server/upgrade/sync.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ impl<S, B> WsUpgrade<S, B>
119119
/// ```rust,no_run
120120
/// use std::net::TcpListener;
121121
/// use std::net::TcpStream;
122-
/// use websocket::server::upgrade::IntoWs;
123-
/// use websocket::Client;
122+
/// use websocket::sync::server::upgrade::IntoWs;
123+
/// use websocket::sync::Client;
124124
///
125125
/// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
126126
///
@@ -221,8 +221,8 @@ impl<S> IntoWs for RequestStreamPair<S>
221221
/// # fn main() {
222222
/// use hyper::server::{Server, Request, Response};
223223
/// use websocket::Message;
224-
/// use websocket::server::upgrade::IntoWs;
225-
/// use websocket::server::upgrade::from_hyper::HyperRequest;
224+
/// use websocket::sync::server::upgrade::IntoWs;
225+
/// use websocket::sync::server::upgrade::HyperRequest;
226226
///
227227
/// Server::http("0.0.0.0:80").unwrap().handle(move |req: Request, res: Response| {
228228
/// match HyperRequest(req).into_ws() {

src/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Provides the default stream type for WebSocket connections.
22
3-
use std::io::{self, Read, Write};
3+
use std::io::{Read, Write};
44

55
/// Represents a stream that can be read from, and written to.
66
/// This is an abstraction around readable and writable things to be able

0 commit comments

Comments
 (0)