wsprism_gateway/services/
echo_binary.rs

1use async_trait::async_trait;
2
3use wsprism_core::error::Result;
4use wsprism_core::protocol::hot::HotFrame;
5
6use crate::dispatch::BinaryService;
7use crate::realtime::{Outgoing, Payload, QoS, RealtimeCtx};
8
9/// Echo binary frames back to the active room or the current session.
10///
11/// Useful for proving Hot Lane routing and session scoping.
12pub struct EchoBinaryService {
13    svc_id: u8,
14}
15
16impl EchoBinaryService {
17    pub fn new(svc_id: u8) -> Self {
18        Self { svc_id }
19    }
20}
21
22#[async_trait]
23impl BinaryService for EchoBinaryService {
24    fn svc_id(&self) -> u8 {
25        self.svc_id
26    }
27
28    async fn handle_binary(&self, ctx: RealtimeCtx, frame: HotFrame) -> Result<()> {
29        let out = Outgoing {
30            qos: QoS::Lossy,
31            payload: Payload::Binary(frame.payload.clone()),
32        };
33
34        if let Some(room) = ctx.active_room() {
35            // room-based publish
36            ctx.publish_room_lossy(room, out)?;
37            Ok(())
38        } else {
39            // roomless: only echo to current session
40            ctx.send_to_session(out)?;
41            Ok(())
42        }
43    }
44}