|
|
@ -18,16 +18,14 @@ extern crate url; |
|
|
|
|
|
|
|
|
|
|
|
use std::env; |
|
|
|
use std::env; |
|
|
|
use std::io::{self, Read, Write}; |
|
|
|
use std::io::{self, Read, Write}; |
|
|
|
use std::net::ToSocketAddrs; |
|
|
|
|
|
|
|
use std::thread; |
|
|
|
use std::thread; |
|
|
|
|
|
|
|
|
|
|
|
use futures::sync::mpsc; |
|
|
|
use futures::sync::mpsc; |
|
|
|
use futures::{Future, Sink, Stream}; |
|
|
|
use futures::{Future, Sink, Stream}; |
|
|
|
use tokio_core::net::TcpStream; |
|
|
|
|
|
|
|
use tokio_core::reactor::Core; |
|
|
|
use tokio_core::reactor::Core; |
|
|
|
use tungstenite::protocol::Message; |
|
|
|
use tungstenite::protocol::Message; |
|
|
|
|
|
|
|
|
|
|
|
use tokio_tungstenite::client_async; |
|
|
|
use tokio_tungstenite::connect_async; |
|
|
|
|
|
|
|
|
|
|
|
fn main() { |
|
|
|
fn main() { |
|
|
|
// Specify the server address to which the client will be connecting.
|
|
|
|
// 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") |
|
|
|
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 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.
|
|
|
|
// Create the event loop and initiate the connection to the remote server.
|
|
|
|
let mut core = Core::new().unwrap(); |
|
|
|
let mut core = Core::new().unwrap(); |
|
|
|
let handle = core.handle(); |
|
|
|
let handle = core.handle(); |
|
|
|
let tcp = TcpStream::connect(&addr, &handle); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Right now Tokio doesn't support a handle to stdin running on the event
|
|
|
|
// 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
|
|
|
|
// 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
|
|
|
|
// 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.
|
|
|
|
// more work from the remote then we can exit.
|
|
|
|
let mut stdout = io::stdout(); |
|
|
|
let mut stdout = io::stdout(); |
|
|
|
let client = tcp.and_then(|stream| { |
|
|
|
let client = connect_async(url, handle.remote().clone()).and_then(|ws_stream| { |
|
|
|
client_async(url, stream).and_then(|ws_stream| { |
|
|
|
println!("WebSocket handshake has been successfully completed"); |
|
|
|
println!("WebSocket handshake has been successfully completed"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// `sink` is the stream of messages going out.
|
|
|
|
// `sink` is the stream of messages going out.
|
|
|
|
// `stream` is the stream of incoming messages.
|
|
|
|
// `stream` is the stream of incoming messages.
|
|
|
|
let (sink, stream) = ws_stream.split(); |
|
|
|
let (sink, stream) = ws_stream.split(); |
|
|
|
|
|
|
|
|
|
|
|
// We forward all messages, composed out of the data, entered to
|
|
|
|
// We forward all messages, composed out of the data, entered to
|
|
|
|
// the stdin, to the `sink`.
|
|
|
|
// the stdin, to the `sink`.
|
|
|
|
let send_stdin = stdin_rx.forward(sink); |
|
|
|
let send_stdin = stdin_rx.forward(sink); |
|
|
|
let write_stdout = stream.for_each(|message| { |
|
|
|
let write_stdout = stream.for_each(|message| { |
|
|
|
stdout.write_all(&message.into_data()).unwrap(); |
|
|
|
stdout.write_all(&message.into_data()).unwrap(); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Wait for either of futures to complete.
|
|
|
|
// Wait for either of futures to complete.
|
|
|
|
send_stdin.map(|_| ()) |
|
|
|
send_stdin.map(|_| ()) |
|
|
|
.select(write_stdout.map(|_| ())) |
|
|
|
.select(write_stdout.map(|_| ())) |
|
|
|
.then(|_| Ok(())) |
|
|
|
.then(|_| Ok(())) |
|
|
|
}).map_err(|e| { |
|
|
|
}).map_err(|e| { |
|
|
|
println!("Error during the websocket handshake occurred: {}", e); |
|
|
|
println!("Error during the websocket handshake occurred: {}", e); |
|
|
|
io::Error::new(io::ErrorKind::Other, e) |
|
|
|
io::Error::new(io::ErrorKind::Other, e) |
|
|
|
}) |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// And now that we've got our client, we execute it in the event loop!
|
|
|
|
// And now that we've got our client, we execute it in the event loop!
|
|
|
|