feat: add warp_output

This commit is contained in:
2021-07-03 19:10:33 +08:00
parent cd8afe953c
commit 144ed41e35

View File

@@ -11,30 +11,33 @@ use url::Url;
use rustyline::Editor; use rustyline::Editor;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use std::time::Duration; use std::time::Duration;
use rust_util::util_msg::{clear_lastline, flush_stdout};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let matches = App::new("") let matches = App::new("")
.arg(Arg::with_name("connect").short("c").long("connect").takes_value(true).help("WebSocket URL")) .arg(Arg::with_name("connect").short("c").long("connect").takes_value(true).help("WebSocket URL"))
.arg(Arg::with_name("disable-ping-pong").long("disable-ping-pong").help("Disable ping/pong"))
.get_matches(); .get_matches();
let connect = matches.value_of("connect").unwrap_or_else(|| { let connect = matches.value_of("connect").unwrap_or_else(|| {
failure_and_exit!("Arg --connect is required."); failure_and_exit!("Arg --connect is required.");
}); });
let enable_ping_pong = !matches.is_present("disable-ping-pong");
let url = Url::parse(connect).unwrap_or_else(|e| { let url = Url::parse(connect).unwrap_or_else(|e| {
failure_and_exit!("Parse connect url: {}, failed: {}", connect, e); failure_and_exit!("Parse connect url: {}, failed: {}", connect, e);
}); });
debugging!("Connecting to: {}", url); debugging!("Connecting to: {}", url);
let r = connect_to_and_loop(&url).await; let r = connect_to_and_loop(&url, enable_ping_pong).await;
match r { match r {
Ok(_) => success!("Success"), Ok(_) => success!("Success"),
Err(e) => failure!("Error: {}", e), Err(e) => failure!("Error: {}", e),
} }
} }
async fn connect_to_and_loop(url: &Url) -> XResult<()> { async fn connect_to_and_loop(url: &Url, enable_ping_pong: bool) -> XResult<()> {
let (ws_stream, _) = connect_async(url).await?; let (ws_stream, _) = connect_async(url).await?;
let (mut ws_write, mut ws_read) = ws_stream.split(); let (mut ws_write, mut ws_read) = ws_stream.split();
@@ -60,7 +63,6 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
Ok(line) => { Ok(line) => {
if !line.is_empty() { if !line.is_empty() {
rl.add_history_entry(line.as_str()); rl.add_history_entry(line.as_str());
println!("Input line: {}", line);
let cloned_sender = cloned_sender.clone(); let cloned_sender = cloned_sender.clone();
rt.spawn(async move { rt.spawn(async move {
if let Err(e) = cloned_sender.send(Message::Text(line)).await { if let Err(e) = cloned_sender.send(Message::Text(line)).await {
@@ -85,17 +87,22 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
} }
}); });
if !enable_ping_pong {
wrap_output(|| debugging!("Ping/Pong is disabled"));
} else {
wrap_output(|| debugging!("Ping/Pong is enabled"));
let time_cloned_sender = sender.clone(); let time_cloned_sender = sender.clone();
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
tokio::time::sleep(Duration::from_secs(10)).await; tokio::time::sleep(Duration::from_secs(10)).await;
debugging!("Send ping message"); wrap_output(|| debugging!("Send ping message"));
if let Err(e) = time_cloned_sender.send(Message::Ping(vec![])).await { if let Err(e) = time_cloned_sender.send(Message::Ping(vec![])).await {
debugging!("Send ping message failed: {}", e); wrap_output(|| debugging!("Send ping message failed: {}", e));
return; return;
} }
} }
}); });
}
loop { loop {
let next = ws_read.next().await; let next = ws_read.next().await;
@@ -110,27 +117,31 @@ async fn connect_to_and_loop(url: &Url) -> XResult<()> {
Some(Ok(message)) => { Some(Ok(message)) => {
match message { match message {
Message::Ping(msg) => { Message::Ping(msg) => {
debugging!("Received ping message: {:?}", msg); wrap_output(|| debugging!("Received ping message: {:?}", msg));
let r = sender.send(Message::Pong(msg)).await; let r = sender.send(Message::Pong(msg)).await;
r.ok(); r.ok();
} }
Message::Pong(msg) => { Message::Pong(msg) => {
debugging!("Received pong message: {:?}", msg); wrap_output(|| debugging!("Received pong message: {:?}", msg));
} }
Message::Close(close_frame) => { Message::Close(close_frame) => {
information!("WebSocket closed: {:?}", close_frame); wrap_output(|| information!("WebSocket closed: {:?}", close_frame));
} }
Message::Binary(msg) => { Message::Binary(msg) => {
information!("Received binary message: {:?}", msg); wrap_output(|| information!("Received binary message: {:?}", msg));
} }
Message::Text(msg) => { Message::Text(msg) => {
information!("Received text message: {}", msg); wrap_output(|| information!("Received text message: {}", msg));
} }
} }
} }
} }
} }
}
// t.join().unwrap();
// Ok(()) fn wrap_output<F>(f: F) where F: Fn() -> () {
clear_lastline();
f();
print!("ws $ ");
flush_stdout();
} }