api/ws/tickets/
ws_handlers.rs

1use serde_json::json;
2use util::ws::handler_trait::WsHandler;
3use util::ws::runtime::WsContext;
4use util::ws::handler_trait::async_trait;
5use super::common::TicketIncoming;
6
7pub struct TicketWsHandler;
8
9#[async_trait]
10impl WsHandler for TicketWsHandler {
11    type In = TicketIncoming;
12
13    async fn on_open(&self, _ctx: &WsContext) {
14        // Optional: announce join, preload state, etc.
15        // _ctx.emit("joined", &json!({ "ok": true })).await;
16    }
17
18    async fn on_message(&self, ctx: &WsContext, msg: Self::In) {
19        match msg {
20            TicketIncoming::Typing { sender } => {
21                // Broadcast "typing" on the topic using your standard envelope
22                ctx.emit("typing", &json!({ "sender": sender })).await;
23            }
24            TicketIncoming::Ping => {
25                // App-level pong to THIS client (framework also auto-responds to {"type":"ping"})
26                let _ = ctx.reply_text(json!({
27                    "event": "pong",
28                    "topic": ctx.topic,
29                    "payload": {},
30                    "ts": chrono::Utc::now().to_rfc3339(),
31                }).to_string()).await;
32            }
33        }
34    }
35
36    async fn on_close(&self, _ctx: &WsContext) {
37        // Optional: cleanup
38    }
39}