|
| 1 | +//! Minimal runnable WebSocket **server** demo (doc 039-validation Layer 5) — the |
| 2 | +//! first real consumer of the connector and a manual-smoke vehicle. |
| 3 | +//! |
| 4 | +//! Run: |
| 5 | +//! ```text |
| 6 | +//! cargo run -p aimdb-websocket-connector --example ws_server |
| 7 | +//! ``` |
| 8 | +//! Then connect a client and subscribe to the ticking `counter` record: |
| 9 | +//! ```text |
| 10 | +//! wscat -c ws://127.0.0.1:8080/ws |
| 11 | +//! > {"type":"subscribe","topics":["counter"]} |
| 12 | +//! < {"type":"subscribed","topics":["counter"]} |
| 13 | +//! < {"type":"data","topic":"counter","payload":{"n":1},"ts":...} |
| 14 | +//! ``` |
| 15 | +//! Or write to the inbound `echo` record: |
| 16 | +//! ```text |
| 17 | +//! > {"type":"write","topic":"echo","payload":{"msg":"hi"}} |
| 18 | +//! ``` |
| 19 | +
|
| 20 | +use std::sync::Arc; |
| 21 | +use std::time::Duration; |
| 22 | + |
| 23 | +use aimdb_core::buffer::BufferCfg; |
| 24 | +use aimdb_core::AimDbBuilder; |
| 25 | +use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; |
| 26 | +use aimdb_websocket_connector::WebSocketConnector; |
| 27 | +use serde::{Deserialize, Serialize}; |
| 28 | +use serde_json::json; |
| 29 | + |
| 30 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 31 | +struct Counter { |
| 32 | + n: u64, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 36 | +struct Echo { |
| 37 | + msg: String, |
| 38 | +} |
| 39 | + |
| 40 | +#[tokio::main] |
| 41 | +async fn main() { |
| 42 | + let addr = "127.0.0.1:8080"; |
| 43 | + |
| 44 | + let mut sb = AimDbBuilder::new() |
| 45 | + .runtime(Arc::new(TokioAdapter)) |
| 46 | + .with_connector( |
| 47 | + WebSocketConnector::new() |
| 48 | + .bind(addr) |
| 49 | + .path("/ws") |
| 50 | + .with_late_join(true), |
| 51 | + ); |
| 52 | + |
| 53 | + // Outbound: pushed to every subscribed client. |
| 54 | + sb.configure::<Counter>("counter", |reg| { |
| 55 | + reg.buffer(BufferCfg::SingleLatest) |
| 56 | + .with_remote_access() |
| 57 | + .link_to("ws://counter") |
| 58 | + .with_serializer_raw(|c: &Counter| Ok(serde_json::to_vec(c).unwrap())) |
| 59 | + .finish(); |
| 60 | + }); |
| 61 | + // Inbound: clients may `write` to it. |
| 62 | + sb.configure::<Echo>("echo", |reg| { |
| 63 | + reg.buffer(BufferCfg::SingleLatest) |
| 64 | + .with_remote_access() |
| 65 | + .link_from("ws://echo") |
| 66 | + .with_deserializer_raw(|d: &[u8]| { |
| 67 | + serde_json::from_slice::<Echo>(d).map_err(|e| e.to_string()) |
| 68 | + }) |
| 69 | + .finish(); |
| 70 | + }); |
| 71 | + |
| 72 | + let (db, runner) = sb.build().await.expect("build db"); |
| 73 | + let db = Arc::new(db); |
| 74 | + tokio::spawn(runner.run()); |
| 75 | + |
| 76 | + println!("WS server listening on ws://{addr}/ws"); |
| 77 | + println!(" subscribe: wscat -c ws://{addr}/ws → {{\"type\":\"subscribe\",\"topics\":[\"counter\"]}}"); |
| 78 | + |
| 79 | + let mut n = 0u64; |
| 80 | + loop { |
| 81 | + n += 1; |
| 82 | + db.set_record_from_json("counter", json!({ "n": n })) |
| 83 | + .expect("set counter"); |
| 84 | + if let Some(echo) = db.try_latest_as_json("echo") { |
| 85 | + println!("echo record = {echo}"); |
| 86 | + } |
| 87 | + tokio::time::sleep(Duration::from_secs(1)).await; |
| 88 | + } |
| 89 | +} |
0 commit comments