|
|
|
@ -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,8 +63,7 @@ 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| { |
|
|
|
|
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.
|
|
|
|
@ -91,7 +85,6 @@ fn main() { |
|
|
|
|
}).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!
|
|
|
|
|