Skip to content

Commit

Permalink
wip: sketch spawning processes
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed Jun 6, 2024
1 parent 1011abf commit 14ff264
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,54 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
});
}

use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};

tokio::spawn(async move {
let origin = "wss://gateway.discord.gg";
let command = format!(
"websocat {} --ping-interval 5 --ping-timeout 10 -E -t",
origin
);
let _ = tokio::process::Command::new("sh")
let mut child = tokio::process::Command::new("sh")
.arg("-c")
.arg(command)
.arg(&command)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("Failed to spawn command");

let mut stdin = child.stdin.take().expect("Failed to open stdin");
let stdout = child.stdout.take().expect("Failed to open stdout");

tokio::spawn(async move {
loop {
if let Err(e) = stdin.write_all(b"{\"op\":1,\"d\":null}\n").await {
eprintln!("Failed to write to stdin: {}", e);
break;
}
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
});

tokio::spawn(async move {
let mut reader = BufReader::new(stdout);
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line).await {
Ok(0) => break, // EOF
Ok(_) => {
eprint!("{}", line);
}
Err(e) => {
eprintln!("Failed to read from stdout: {}", e);
break;
}
}
}
});

let _ = child.wait().await;
});

xs::api::serve(store).await
Expand Down

0 comments on commit 14ff264

Please sign in to comment.