BlueGit

ict rusty-teams

Files

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

native_trouter_probe.rs

native_trouter_probe.rs 1.5 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};
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(&endpoint).await.expect("browser");
12 let config = RealtimeConfig { native: true, initially_active: true, ..RealtimeConfig::default() };
13 let mut realtime = Realtime::start_with(&session, config).await.expect("realtime start");
14 println!("host {}", realtime.host());
15 let watch = async {
16 while let Some(event) = realtime.recv().await {
17 let label = match &event {
18 RealtimeEvent::Status(status) => format!("status {:?} {}", status.kind, status.detail),
19 RealtimeEvent::Endpoint(_) => "endpoint".to_owned(),
20 RealtimeEvent::Message(message) => format!("message {:?}", message.kind),
21 RealtimeEvent::Presence(entries) => format!("presence x{}", entries.len()),
22 RealtimeEvent::Typing(_) => "typing".to_owned(),
23 };
24 println!("{label}");
25 }
26 };
27 let _ = tokio::time::timeout(Duration::from_secs(seconds), watch).await;
28 realtime.stop().await;
29 println!("stopped");
30}