Created
January 20, 2024 01:30
-
-
Save sigaloid/d0e2a7eb42fed8c2397fbf8423999035 to your computer and use it in GitHub Desktop.
Example async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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