From 7fbf836851076ca5cea6849c782e7329636522d1 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Mon, 22 May 2017 22:55:46 +0200 Subject: [PATCH] Update example for new API. Signed-off-by: Alexey Galakhov --- examples/client.rs | 47 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index d432849..f02afd6 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -18,16 +18,14 @@ extern crate url; use std::env; use std::io::{self, Read, Write}; -use std::net::ToSocketAddrs; use std::thread; use futures::sync::mpsc; use futures::{Future, Sink, Stream}; -use tokio_core::net::TcpStream; use tokio_core::reactor::Core; use tungstenite::protocol::Message; -use tokio_tungstenite::client_async; +use tokio_tungstenite::connect_async; fn main() { // Specify the server address to which the client will be connecting. @@ -35,14 +33,11 @@ fn main() { panic!("this program requires at least one argument") }); - // Get a first IP address of the server from the server URL. let url = url::Url::parse(&connect_addr).unwrap(); - let addr = url.to_socket_addrs().unwrap().next().unwrap(); // Create the event loop and initiate the connection to the remote server. let mut core = Core::new().unwrap(); let handle = core.handle(); - let tcp = TcpStream::connect(&addr, &handle); // Right now Tokio doesn't support a handle to stdin running on the event // loop, so we farm out that work to a separate thread. This thread will @@ -68,30 +63,28 @@ fn main() { // finishes. If we don't have any more data to read or we won't receive any // more work from the remote then we can exit. let mut stdout = io::stdout(); - let client = tcp.and_then(|stream| { - client_async(url, stream).and_then(|ws_stream| { - println!("WebSocket handshake has been successfully completed"); + let client = connect_async(url, handle.remote().clone()).and_then(|ws_stream| { + println!("WebSocket handshake has been successfully completed"); - // `sink` is the stream of messages going out. - // `stream` is the stream of incoming messages. - let (sink, stream) = ws_stream.split(); + // `sink` is the stream of messages going out. + // `stream` is the stream of incoming messages. + let (sink, stream) = ws_stream.split(); - // We forward all messages, composed out of the data, entered to - // the stdin, to the `sink`. - let send_stdin = stdin_rx.forward(sink); - let write_stdout = stream.for_each(|message| { - stdout.write_all(&message.into_data()).unwrap(); - Ok(()) - }); + // We forward all messages, composed out of the data, entered to + // the stdin, to the `sink`. + let send_stdin = stdin_rx.forward(sink); + let write_stdout = stream.for_each(|message| { + stdout.write_all(&message.into_data()).unwrap(); + Ok(()) + }); - // Wait for either of futures to complete. - send_stdin.map(|_| ()) - .select(write_stdout.map(|_| ())) - .then(|_| Ok(())) - }).map_err(|e| { - println!("Error during the websocket handshake occurred: {}", e); - io::Error::new(io::ErrorKind::Other, e) - }) + // Wait for either of futures to complete. + send_stdin.map(|_| ()) + .select(write_stdout.map(|_| ())) + .then(|_| Ok(())) + }).map_err(|e| { + println!("Error during the websocket handshake occurred: {}", e); + io::Error::new(io::ErrorKind::Other, e) }); // And now that we've got our client, we execute it in the event loop!