add default url

This commit is contained in:
Adam 2024-04-26 23:54:22 -04:00
parent 58cf288a90
commit bf88707844

View file

@ -25,35 +25,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// sleep to avoid racing the server on startup when using cargo watch // sleep to avoid racing the server on startup when using cargo watch
sleep(Duration::from_millis(200)).await; sleep(Duration::from_millis(200)).await;
if let Some(connect_addr) = env::args().nth(1) { let connect_addr = env::args()
let url = url::Url::parse(&connect_addr)?; .nth(1)
.unwrap_or_else(|| "ws://127.0.0.1:8080".to_string());
let url = url::Url::parse(&connect_addr)?;
let (stdin_tx, stdin_rx) = futures_channel::mpsc::unbounded(); let (stdin_tx, stdin_rx) = futures_channel::mpsc::unbounded();
tokio::spawn(read_stdin(stdin_tx)); tokio::spawn(read_stdin(stdin_tx));
let (ws_stream, res) = connect_async(url).await?; let (ws_stream, res) = connect_async(url).await?;
println!( println!(
"WebSocket handshake has been successfully completed\n{:#?}", "WebSocket handshake has been successfully completed\n{:#?}",
res res
); );
let (sink, stream) = ws_stream.split(); let (sink, stream) = ws_stream.split();
let stdin_to_ws = stdin_rx.map(Ok).forward(sink); let stdin_to_ws = stdin_rx.map(Ok).forward(sink);
let ws_to_stdout = { let ws_to_stdout = {
stream.for_each(|message| async { stream.for_each(|message| async {
let data = message.unwrap().into_data(); let data = message.unwrap().into_data();
tokio::io::stdout().write_all(&data).await.unwrap(); tokio::io::stdout().write_all(&data).await.unwrap();
}) })
}; };
pin_mut!(stdin_to_ws, ws_to_stdout); pin_mut!(stdin_to_ws, ws_to_stdout);
future::select(stdin_to_ws, ws_to_stdout).await; future::select(stdin_to_ws, ws_to_stdout).await;
} else {
eprintln!("This program requires at least one argument!")
}
Ok(()) Ok(())
} }