Skip to content

Instantly share code, notes, and snippets.

@sigaloid
Created January 20, 2024 01:30
Show Gist options
  • Save sigaloid/d0e2a7eb42fed8c2397fbf8423999035 to your computer and use it in GitHub Desktop.
Save sigaloid/d0e2a7eb42fed8c2397fbf8423999035 to your computer and use it in GitHub Desktop.
Example async
use futures_util::StreamExt;
use tokio_tungstenite::{connect_async, tungstenite::client::IntoClientRequest};
use url::Url;
#[tokio::main]
async fn main() {
// Include file servers.txt from current directory
let urls = include_str!("servers.txt")
.split("\n")
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
for url in urls {
tokio::spawn(listen(url));
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
// Wait forever
tokio::signal::ctrl_c().await.unwrap();
}
async fn listen(url: &str) {
let req = Url::parse(url).unwrap().into_client_request().unwrap();
let (mut ws_stream, _) = connect_async(req).await.expect("Can't connect");
while let Some(Ok(msg)) = ws_stream.next().await {
println!("{}", msg.to_string());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment