BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / examples / native_trouter_probe.rs

native_trouter_probe.rs

native_trouter_probe.rs 1.7 KiB Download Raw
1//! Connects the native Trouter client and prints event kinds (no content) for the given seconds.
2use std::time::Duration;
3
4use chatsvc::{Realtime, RealtimeConfig, RealtimeEvent};
5use session::{DEFAULT_ENDPOINT, Session, SessionConfig};
6
7#[tokio::main]
8async 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}