Remote playback control for any local player — inspired by "Spotify Connect".
at main 55 lines 1.6 kB view raw
1use std::thread; 2 3use owo_colors::OwoColorize; 4use websocket::connect_to_rocksky_websocket; 5 6pub mod players; 7pub mod websocket; 8 9#[tokio::main] 10async fn main() -> Result<(), Box<dyn std::error::Error>> { 11 let home = dirs::home_dir().unwrap(); 12 let token_file = home.join(".rocksky").join("token.json"); 13 14 if !token_file.exists() { 15 println!( 16 "Please run {} to authenticate with Rocksky before connecting to the WebSocket", 17 "`rocksky login`".magenta() 18 ); 19 return Ok(()); 20 } 21 22 let token = std::fs::read_to_string(token_file)?; 23 let token: serde_json::Value = serde_json::from_str(&token)?; 24 let token = token 25 .get("token") 26 .and_then(|t| t.as_str()) 27 .ok_or("Token not found")? 28 .to_string(); 29 30 thread::spawn(move || { 31 let rt = tokio::runtime::Runtime::new().unwrap(); 32 rt.block_on(async move { 33 let delay = 3; 34 35 loop { 36 match connect_to_rocksky_websocket(token.clone()).await { 37 Ok(_) => { 38 println!("WebSocket session ended cleanly"); 39 } 40 Err(e) => { 41 eprintln!("WebSocket session error: {}", e); 42 } 43 } 44 45 println!("Reconnecting in {} seconds...", delay); 46 tokio::time::sleep(std::time::Duration::from_secs(delay)).await; 47 } 48 }) 49 }); 50 51 // Keep the main thread alive to allow the WebSocket to run 52 loop { 53 std::thread::park(); 54 } 55}