BlueGit

ict rusty-teams

Files

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

native_trouter_probe.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
native_trouter_probe.rs 1.9 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()
10 .nth(1)
11 .and_then(|value| value.parse().ok())
12 .unwrap_or(60);
13 let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned());
14 let session = Session::connect_with(
15 &endpoint,
16 SessionConfig {
17 native_http: std::env::var_os("PAGE_FETCH").is_none(),
18 ..SessionConfig::default()
19 },
20 )
21 .await
22 .expect("browser");
23 let ringable = std::env::var_os("RINGABLE").is_some();
24 let config = RealtimeConfig {
25 native: true,
26 initially_active: true,
27 forward_callbacks: ringable,
28 ringable,
29 ..RealtimeConfig::default()
30 };
31 let mut realtime = Realtime::start_with(&session, config)
32 .await
33 .expect("realtime start");
34 println!("host {}", realtime.host());
35 let watch = async {
36 while let Some(event) = realtime.recv().await {
37 let label = match &event {
38 RealtimeEvent::Status(status) => {
39 format!("status {:?} {}", status.kind, status.detail)
40 }
41 RealtimeEvent::Endpoint(_) => "endpoint".to_owned(),
42 RealtimeEvent::Message(message) => format!("message {:?}", message.kind),
43 RealtimeEvent::Presence(entries) => format!("presence x{}", entries.len()),
44 RealtimeEvent::Typing(_) => "typing".to_owned(),
45 };
46 println!("{label}");
47 }
48 };
49 let _ = tokio::time::timeout(Duration::from_secs(seconds), watch).await;
50 realtime.stop().await;
51 println!("stopped");
52}