feat: works

This commit is contained in:
2021-07-03 18:55:49 +08:00
parent b717c864ac
commit cd8afe953c
3 changed files with 202 additions and 16 deletions

163
Cargo.lock generated
View File

@@ -97,6 +97,22 @@ dependencies = [
"winapi",
]
[[package]]
name = "core-foundation"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a89e2ae426ea83155dccf10c0fa6b1463ef6d5fcb44cee0b224a408fa640a62"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "cpufeatures"
version = "0.1.4"
@@ -168,6 +184,21 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foreign-types"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [
"foreign-types-shared",
]
[[package]]
name = "foreign-types-shared"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "form_urlencoded"
version = "1.0.1"
@@ -357,6 +388,24 @@ dependencies = [
"winapi",
]
[[package]]
name = "native-tls"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8d96b2e1c8da3957d58100b09f102c6d9cfdfced01b7ec5a8974044bb09dbd4"
dependencies = [
"lazy_static",
"libc",
"log",
"openssl",
"openssl-probe",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "nibble_vec"
version = "0.1.0"
@@ -409,6 +458,39 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549430950c79ae24e6d02e0b7404534ecf311d94cc9f861e9e4020187d13d885"
dependencies = [
"bitflags",
"cfg-if",
"foreign-types",
"libc",
"once_cell",
"openssl-sys",
]
[[package]]
name = "openssl-probe"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a"
[[package]]
name = "openssl-sys"
version = "0.9.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a7907e3bfa08bb85105209cdfcb6c63d109f8f6c1ed6ca318fff5c1853fbc1d"
dependencies = [
"autocfg",
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.11.1"
@@ -472,6 +554,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
@@ -565,6 +653,15 @@ dependencies = [
"redox_syscall",
]
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]]
name = "rust_util"
version = "0.6.39"
@@ -607,12 +704,45 @@ dependencies = [
"winapi",
]
[[package]]
name = "schannel"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75"
dependencies = [
"lazy_static",
"winapi",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "security-framework"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467"
dependencies = [
"bitflags",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e4effb91b4b8b6fb7732e670b6cee160278ff8e6bf485c7805d9e319d76e284"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "sha-1"
version = "0.9.6"
@@ -670,6 +800,20 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tempfile"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if",
"libc",
"rand",
"redox_syscall",
"remove_dir_all",
"winapi",
]
[[package]]
name = "term"
version = "0.7.0"
@@ -766,6 +910,16 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-tungstenite"
version = "0.14.0"
@@ -774,8 +928,10 @@ checksum = "1e96bb520beab540ab664bd5a9cfeaa1fcd846fa68c830b42e2c8963071251d2"
dependencies = [
"futures-util",
"log",
"native-tls",
"pin-project",
"tokio",
"tokio-native-tls",
"tungstenite",
]
@@ -792,6 +948,7 @@ dependencies = [
"httparse",
"input_buffer",
"log",
"native-tls",
"rand",
"sha-1",
"thiserror",
@@ -865,6 +1022,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"

View File

@@ -13,6 +13,5 @@ rust_util = "0.6.39"
rustyline = "8.2.0"
futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] }
tokio = { version = "1.0.0", features = ["full"] }
tokio-tungstenite = "0.14.0"
tokio-tungstenite = { version = "0.14.0", features = ["native-tls"] }

View File

@@ -10,6 +10,7 @@ use tokio::sync::mpsc::channel;
use url::Url;
use rustyline::Editor;
use rustyline::error::ReadlineError;
use std::time::Duration;
#[tokio::main]
async fn main() {
@@ -25,6 +26,7 @@ async fn main() {
failure_and_exit!("Parse connect url: {}, failed: {}", connect, e);
});
debugging!("Connecting to: {}", url);
let r = connect_to_and_loop(&url).await;
match r {
Ok(_) => success!("Success"),
@@ -48,31 +50,53 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
}
});
let t = std::thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().unwrap();
let cloned_sender = sender.clone();
let _t = std::thread::spawn(move || {
let mut rl = Editor::<()>::new();
loop {
let readline = rl.readline("ws $ ");
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str());
println!("Input line: {}", line);
},
if !line.is_empty() {
rl.add_history_entry(line.as_str());
println!("Input line: {}", line);
let cloned_sender = cloned_sender.clone();
rt.spawn(async move {
if let Err(e) = cloned_sender.send(Message::Text(line)).await {
failure!("Send message failed: {}", e);
}
});
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
break
},
information!("Press Ctrl+C");
std::process::exit(1);
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
break
},
break;
}
Err(err) => {
println!("Error: {:?}", err);
break
break;
}
}
}
});
let time_cloned_sender = sender.clone();
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
debugging!("Send ping message");
if let Err(e) = time_cloned_sender.send(Message::Ping(vec![])).await {
debugging!("Send ping message failed: {}", e);
return;
}
}
});
loop {
let next = ws_read.next().await;
match next {
@@ -86,12 +110,12 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
Some(Ok(message)) => {
match message {
Message::Ping(msg) => {
information!("Received ping message: {:?}", msg);
debugging!("Received ping message: {:?}", msg);
let r = sender.send(Message::Pong(msg)).await;
r.ok();
}
Message::Pong(msg) => {
information!("Received pong message: {:?}", msg);
debugging!("Received pong message: {:?}", msg);
}
Message::Close(close_frame) => {
information!("WebSocket closed: {:?}", close_frame);
@@ -107,6 +131,6 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
}
}
t.join().unwrap();
Ok(())
// t.join().unwrap();
// Ok(())
}