From 60619cd71f93de15244ee8f4684333d8cc5fe7b2 Mon Sep 17 00:00:00 2001 From: tlietz Date: Sat, 1 Jan 2022 23:26:12 -0600 Subject: [PATCH] adds connect_nonblocking and connect_nonblocking_with_config functions --- src/client.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/client.rs b/src/client.rs index 12dfe9b..f210e5c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -119,6 +119,48 @@ pub fn connect( connect_with_config(request, None, 3) } +/// Connect to the given WebSocket in non-blocking mode. +/// +/// Uses a websocket configuration passed as an argument to the function. Calling it with `None` is +/// equal to calling the `connect_nonblocking()` function. +/// +/// The URL may be either ws:// or wss://. +/// To support wss:// URLs, feature `native-tls` or `rustls-tls` must be turned on. +/// +/// This function "just works" for those who want a simple non-blocking solution +/// similar to `std::net::TcpStream`. If you want a custom stream, call `client` instead. +/// +/// This function uses `native_tls` or `rustls` to do TLS depending on the feature flags enabled. If +/// you want to use other TLS libraries, use `client` instead. There is no need to enable any of +/// the `*-tls` features if you don't call `connect` since it's the only function that uses them. +pub fn connect_nonblocking_with_config( + request: Req, + config: Option, + max_redirects: u8, +) -> Result<(WebSocket>, Response)> { + let (ws, resp) = connect_with_config(request, config, max_redirects)?; + let MaybeTlsStream::Plain(s) = ws.get_ref(); + s.set_nonblocking(true)?; + Ok((ws, resp)) +} + +/// Connect to the given WebSocket in non-blocking mode. +/// +/// The URL may be either ws:// or wss://. +/// To support wss:// URLs, feature `native-tls` or `rustls-tls` must be turned on. +/// +/// This function "just works" for those who want a simple non-blocking solution +/// similar to `std::net::TcpStream`. If you want a custom stream, call `client` instead. +/// +/// This function uses `native_tls` or `rustls` to do TLS depending on the feature flags enabled. If +/// you want to use other TLS libraries, use `client` instead. There is no need to enable any of +/// the `*-tls` features if you don't call `connect` since it's the only function that uses them. +pub fn connect_nonblocking( + request: Req, +) -> Result<(WebSocket>, Response)> { + connect_nonblocking_with_config(request, None, 3) +} + fn connect_to_some(addrs: &[SocketAddr], uri: &Uri) -> Result { for addr in addrs { debug!("Trying to contact {} at {}...", uri, addr);