| 1 | //! Connects the native Trouter client and prints event kinds (no content) for the given seconds. |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use chatsvc::{Realtime, RealtimeConfig, RealtimeEvent}; |
| 5 | use session::{DEFAULT_ENDPOINT, Session, SessionConfig}; |
| 6 | |
| 7 | #[tokio::main] |
| 8 | async fn main() { |
| 9 | let seconds = std::env::args().nth(1).and_then(|value| value.parse().ok()).unwrap_or(60); |
| 10 | let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned()); |
| 11 | let session = Session::connect_with(&endpoint, SessionConfig { native_http: std::env::var_os("PAGE_FETCH").is_none(), ..SessionConfig::default() }).await.expect("browser"); |
| 12 | let ringable = std::env::var_os("RINGABLE").is_some(); |
| 13 | let config = RealtimeConfig { native: true, initially_active: true, forward_callbacks: ringable, ringable, ..RealtimeConfig::default() }; |
| 14 | let mut realtime = Realtime::start_with(&session, config).await.expect("realtime start"); |
| 15 | println!("host {}", realtime.host()); |
| 16 | let watch = async { |
| 17 | while let Some(event) = realtime.recv().await { |
| 18 | let label = match &event { |
| 19 | RealtimeEvent::Status(status) => format!("status {:?} {}", status.kind, status.detail), |
| 20 | RealtimeEvent::Endpoint(_) => "endpoint".to_owned(), |
| 21 | RealtimeEvent::Message(message) => format!("message {:?}", message.kind), |
| 22 | RealtimeEvent::Presence(entries) => format!("presence x{}", entries.len()), |
| 23 | RealtimeEvent::Typing(_) => "typing".to_owned(), |
| 24 | }; |
| 25 | println!("{label}"); |
| 26 | } |
| 27 | }; |
| 28 | let _ = tokio::time::timeout(Duration::from_secs(seconds), watch).await; |
| 29 | realtime.stop().await; |
| 30 | println!("stopped"); |
| 31 | } |