Update example for new API.

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/1/head
Alexey Galakhov 7 years ago
parent 5fbcd8d413
commit 7fbf836851
  1. 47
      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!

Loading…
Cancel
Save