@@ -398,7 +398,7 @@ impl<'u> ClientBuilder<'u> {
398
398
}
399
399
400
400
/// Create an insecure (plain TCP) connection to the client.
401
- /// In this case no `Box` will be used you will just get a TcpStream,
401
+ /// In this case no `Box` will be used, you will just get a TcpStream,
402
402
/// giving you the ability to split the stream into a reader and writer
403
403
/// (since SSL streams cannot be cloned).
404
404
///
@@ -419,9 +419,9 @@ impl<'u> ClientBuilder<'u> {
419
419
}
420
420
421
421
/// Create an SSL connection to the sever.
422
- /// This will only use an `SslStream `, this is useful
422
+ /// This will only use an `TlsStream `, this is useful
423
423
/// when you want to be sure to connect over SSL or when you want access
424
- /// to the `SslStream ` functions (without having to go through a `Box`).
424
+ /// to the `TlsStream ` functions (without having to go through a `Box`).
425
425
#[ cfg( feature="sync-ssl" ) ]
426
426
pub fn connect_secure (
427
427
& mut self ,
@@ -434,7 +434,6 @@ impl<'u> ClientBuilder<'u> {
434
434
self . connect_on ( ssl_stream)
435
435
}
436
436
437
- // TODO: similar ability for server?
438
437
/// Connects to a websocket server on any stream you would like.
439
438
/// Possible streams:
440
439
/// - Unix Sockets
@@ -483,6 +482,50 @@ impl<'u> ClientBuilder<'u> {
483
482
Ok ( Client :: unchecked ( reader, response. headers , true , false ) )
484
483
}
485
484
485
+ /// Connect to a websocket server asynchronously.
486
+ ///
487
+ /// This will use a `Box<AsyncRead + AsyncWrite + Send>` to represent either
488
+ /// an SSL connection or a normal TCP connection, what to use will be decided
489
+ /// using the protocol of the URL passed in (e.g. `ws://` or `wss://`)
490
+ ///
491
+ /// If you have non-default SSL circumstances, you can use the `ssl_config`
492
+ /// parameter to configure those.
493
+ ///
494
+ ///# Example
495
+ ///
496
+ /// ```rust,no_run
497
+ /// # extern crate rand;
498
+ /// # extern crate tokio_core;
499
+ /// # extern crate futures;
500
+ /// # extern crate websocket;
501
+ /// use websocket::ClientBuilder;
502
+ /// use websocket::futures::{Future, Stream, Sink};
503
+ /// use websocket::Message;
504
+ /// use tokio_core::reactor::Core;
505
+ /// # use rand::Rng;
506
+ ///
507
+ /// # fn main() {
508
+ /// let mut core = Core::new().unwrap();
509
+ ///
510
+ /// // let's randomly do either SSL or plaintext
511
+ /// let url = if rand::thread_rng().gen() {
512
+ /// "ws://echo.websocket.org"
513
+ /// } else {
514
+ /// "wss://echo.websocket.org"
515
+ /// };
516
+ ///
517
+ /// // send a message and hear it come back
518
+ /// let echo_future = ClientBuilder::new(url).unwrap()
519
+ /// .async_connect(None, &core.handle())
520
+ /// .and_then(|(s, _)| s.send(Message::text("hallo").into()))
521
+ /// .and_then(|s| s.into_future().map_err(|e| e.0))
522
+ /// .map(|(m, _)| {
523
+ /// assert_eq!(m, Some(Message::text("hallo").into()))
524
+ /// });
525
+ ///
526
+ /// core.run(echo_future).unwrap();
527
+ /// # }
528
+ /// ```
486
529
#[ cfg( feature="async-ssl" ) ]
487
530
pub fn async_connect (
488
531
self ,
@@ -536,6 +579,41 @@ impl<'u> ClientBuilder<'u> {
536
579
}
537
580
}
538
581
582
+ /// Asynchronously create an SSL connection to a websocket sever.
583
+ ///
584
+ /// This method will only try to connect over SSL and fail otherwise, useful
585
+ /// when you want to be sure to connect over SSL or when you want access
586
+ /// to the `TlsStream` functions (without having to go through a `Box`).
587
+ ///
588
+ /// If you have non-default SSL circumstances, you can use the `ssl_config`
589
+ /// parameter to configure those.
590
+ ///
591
+ ///# Example
592
+ ///
593
+ /// ```rust
594
+ /// # extern crate tokio_core;
595
+ /// # extern crate futures;
596
+ /// # extern crate websocket;
597
+ /// use websocket::ClientBuilder;
598
+ /// use websocket::futures::{Future, Stream, Sink};
599
+ /// use websocket::Message;
600
+ /// use tokio_core::reactor::Core;
601
+ /// # fn main() {
602
+ ///
603
+ /// let mut core = Core::new().unwrap();
604
+ ///
605
+ /// // send a message and hear it come back
606
+ /// let echo_future = ClientBuilder::new("wss://echo.websocket.org").unwrap()
607
+ /// .async_connect_secure(None, &core.handle())
608
+ /// .and_then(|(s, _)| s.send(Message::text("hallo").into()))
609
+ /// .and_then(|s| s.into_future().map_err(|e| e.0))
610
+ /// .map(|(m, _)| {
611
+ /// assert_eq!(m, Some(Message::text("hallo").into()))
612
+ /// });
613
+ ///
614
+ /// core.run(echo_future).unwrap();
615
+ /// # }
616
+ /// ```
539
617
#[ cfg( feature="async-ssl" ) ]
540
618
pub fn async_connect_secure (
541
619
self ,
@@ -578,6 +656,38 @@ impl<'u> ClientBuilder<'u> {
578
656
579
657
// TODO: add timeout option for connecting
580
658
// TODO: add conveniences like .response_to_pings, .send_close, etc.
659
+ /// Asynchronously create an insecure (plain TCP) connection to the client.
660
+ ///
661
+ /// In this case no `Box` will be used, you will just get a `TcpStream`,
662
+ /// giving you less allocations on the heap and direct access to `TcpStream`
663
+ /// functions.
664
+ ///
665
+ ///# Example
666
+ ///
667
+ /// ```rust,no_run
668
+ /// # extern crate tokio_core;
669
+ /// # extern crate futures;
670
+ /// # extern crate websocket;
671
+ /// use websocket::ClientBuilder;
672
+ /// use websocket::futures::{Future, Stream, Sink};
673
+ /// use websocket::Message;
674
+ /// use tokio_core::reactor::Core;
675
+ /// # fn main() {
676
+ ///
677
+ /// let mut core = Core::new().unwrap();
678
+ ///
679
+ /// // send a message and hear it come back
680
+ /// let echo_future = ClientBuilder::new("ws://echo.websocket.org").unwrap()
681
+ /// .async_connect_insecure(&core.handle())
682
+ /// .and_then(|(s, _)| s.send(Message::text("hallo").into()))
683
+ /// .and_then(|s| s.into_future().map_err(|e| e.0))
684
+ /// .map(|(m, _)| {
685
+ /// assert_eq!(m, Some(Message::text("hallo").into()))
686
+ /// });
687
+ ///
688
+ /// core.run(echo_future).unwrap();
689
+ /// # }
690
+ /// ```
581
691
#[ cfg( feature="async" ) ]
582
692
pub fn async_connect_insecure ( self , handle : & Handle ) -> async :: ClientNew < async :: TcpStream > {
583
693
let tcp_stream = match self . async_tcpstream ( Some ( false ) , handle) {
@@ -600,6 +710,48 @@ impl<'u> ClientBuilder<'u> {
600
710
Box :: new ( future)
601
711
}
602
712
713
+ /// Asynchronously connects to a websocket server on any stream you would like.
714
+ /// Possible streams:
715
+ /// - Unix Sockets
716
+ /// - Bluetooth
717
+ /// - Logging Middle-ware
718
+ /// - SSH
719
+ ///
720
+ /// The stream must be `AsyncRead + AsyncWrite + Send + 'static`.
721
+ ///
722
+ /// # Example
723
+ ///
724
+ /// ```rust
725
+ /// use websocket::header::WebSocketProtocol;
726
+ /// use websocket::ClientBuilder;
727
+ /// use websocket::sync::stream::ReadWritePair;
728
+ /// use websocket::futures::Future;
729
+ /// use websocket::async::Core;
730
+ /// # use std::io::Cursor;
731
+ ///
732
+ /// let mut core = Core::new().unwrap();
733
+ ///
734
+ /// let accept = b"\
735
+ /// HTTP/1.1 101 Switching Protocols\r\n\
736
+ /// Upgrade: websocket\r\n\
737
+ /// Sec-WebSocket-Protocol: proto-metheus\r\n\
738
+ /// Connection: Upgrade\r\n\
739
+ /// Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\
740
+ /// \r\n";
741
+ ///
742
+ /// let input = Cursor::new(&accept[..]);
743
+ /// let output = Cursor::new(Vec::new());
744
+ ///
745
+ /// let client = ClientBuilder::new("wss://test.ws").unwrap()
746
+ /// .key(b"the sample nonce".clone())
747
+ /// .async_connect_on(ReadWritePair(input, output))
748
+ /// .map(|(_, headers)| {
749
+ /// let proto: &WebSocketProtocol = headers.get().unwrap();
750
+ /// assert_eq!(proto.0.first().unwrap(), "proto-metheus")
751
+ /// });
752
+ ///
753
+ /// core.run(client).unwrap();
754
+ /// ```
603
755
#[ cfg( feature="async" ) ]
604
756
pub fn async_connect_on < S > ( self , stream : S ) -> async :: ClientNew < S >
605
757
where S : stream:: async:: Stream + Send + ' static
0 commit comments