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