Compare commits
No commits in common. "58cf288a90611a6f461698ea0fce1178a1786a53" and "a915b463d1a321f3382b937d743f417b243972a0" have entirely different histories.
58cf288a90
...
a915b463d1
3 changed files with 47 additions and 108 deletions
71
Cargo.lock
generated
71
Cargo.lock
generated
|
@ -40,9 +40,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.5.0"
|
version = "1.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "block-buffer"
|
name = "block-buffer"
|
||||||
|
@ -69,7 +69,6 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
||||||
name = "cards"
|
name = "cards"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
|
||||||
"futures-channel",
|
"futures-channel",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"rand",
|
"rand",
|
||||||
|
@ -142,21 +141,6 @@ dependencies = [
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
|
||||||
dependencies = [
|
|
||||||
"futures-channel",
|
|
||||||
"futures-core",
|
|
||||||
"futures-executor",
|
|
||||||
"futures-io",
|
|
||||||
"futures-sink",
|
|
||||||
"futures-task",
|
|
||||||
"futures-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.30"
|
version = "0.3.30"
|
||||||
|
@ -164,7 +148,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-sink",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -173,34 +156,6 @@ version = "0.3.30"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-executor"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
|
||||||
dependencies = [
|
|
||||||
"futures-core",
|
|
||||||
"futures-task",
|
|
||||||
"futures-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-io"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "futures-macro"
|
|
||||||
version = "0.3.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-sink"
|
name = "futures-sink"
|
||||||
version = "0.3.30"
|
version = "0.3.30"
|
||||||
|
@ -219,13 +174,9 @@ version = "0.3.30"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-channel",
|
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-io",
|
|
||||||
"futures-macro",
|
|
||||||
"futures-sink",
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
"memchr",
|
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
"slab",
|
"slab",
|
||||||
|
@ -305,9 +256,9 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
version = "0.4.12"
|
version = "0.4.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
|
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
"scopeguard",
|
"scopeguard",
|
||||||
|
@ -366,9 +317,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.2"
|
version = "0.12.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7e4af0ca4f6caed20e900d564c242b8e5d4903fdacf31d3daf527b66fe6f42fb"
|
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lock_api",
|
"lock_api",
|
||||||
"parking_lot_core",
|
"parking_lot_core",
|
||||||
|
@ -376,15 +327,15 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot_core"
|
name = "parking_lot_core"
|
||||||
version = "0.9.10"
|
version = "0.9.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
|
checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-targets 0.52.5",
|
"windows-targets 0.48.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -461,9 +412,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.1"
|
version = "0.4.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e"
|
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
|
@ -15,4 +15,3 @@ futures-util = { version = "0.3.30", default-features = false, features = [
|
||||||
tokio = { version = "1.37.0", features = ["full"] }
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
tokio-tungstenite = "*"
|
tokio-tungstenite = "*"
|
||||||
url = "2.5.0"
|
url = "2.5.0"
|
||||||
futures = "0.3.30"
|
|
||||||
|
|
|
@ -1,49 +1,28 @@
|
||||||
use futures_util::{future, pin_mut, StreamExt};
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use tokio::{
|
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
use futures_util::{future, pin_mut, StreamExt};
|
||||||
time::sleep,
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
||||||
|
|
||||||
// Our helper method which will read data from stdin and send it along the
|
|
||||||
// sender provided.
|
|
||||||
async fn read_stdin(tx: futures_channel::mpsc::UnboundedSender<Message>) {
|
|
||||||
let mut stdin = tokio::io::stdin();
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let mut buf = vec![0; 1024];
|
|
||||||
let n = stdin.read(&mut buf).await;
|
|
||||||
buf.truncate(n.unwrap());
|
|
||||||
tx.unbounded_send(Message::binary(buf)).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() {
|
||||||
// sleep to avoid racing the server on startup when using cargo watch
|
let connect_addr = env::args()
|
||||||
sleep(Duration::from_millis(200)).await;
|
.nth(1)
|
||||||
|
.unwrap_or_else(|| panic!("this program requires at least one argument"));
|
||||||
|
|
||||||
if let Some(connect_addr) = env::args().nth(1) {
|
let url = url::Url::parse(&connect_addr).unwrap();
|
||||||
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, _) = connect_async(url).await.expect("Failed to connect");
|
||||||
|
println!("WebSocket handshake has been successfully completed");
|
||||||
|
|
||||||
println!(
|
let (write, read) = ws_stream.split();
|
||||||
"WebSocket handshake has been successfully completed\n{:#?}",
|
|
||||||
res
|
|
||||||
);
|
|
||||||
|
|
||||||
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(write);
|
||||||
let ws_to_stdout = {
|
let ws_to_stdout = {
|
||||||
stream.for_each(|message| async {
|
read.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();
|
||||||
})
|
})
|
||||||
|
@ -51,9 +30,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
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(())
|
// Our helper method which will read data from stdin and send it along the
|
||||||
|
// sender provided.
|
||||||
|
async fn read_stdin(tx: futures_channel::mpsc::UnboundedSender<Message>) {
|
||||||
|
let mut stdin = tokio::io::stdin();
|
||||||
|
loop {
|
||||||
|
let mut buf = vec![0; 1024];
|
||||||
|
let n = match stdin.read(&mut buf).await {
|
||||||
|
Err(_) | Ok(0) => break,
|
||||||
|
Ok(n) => n,
|
||||||
|
};
|
||||||
|
buf.truncate(n);
|
||||||
|
tx.unbounded_send(Message::binary(buf)).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue