SpaceCAN's spacecan-virtual crate includes an Axum WebSocket server that bridges the UDP multicast CAN bus simulation to the React/Vite ground station dashboard running in the browser.
Why WebSocket: The browser cannot join UDP multicast groups — WebSockets are the lowest-latency bidirectional protocol available in the browser. The bridge translates between the binary CAN frame format and JSON messages for the React client.
Architecture:
Virtual CAN nodes ─── UDP multicast ─── Bridge server ─── WebSocket ─── React dashboard
(Tokio tasks) (Axum) (Browser)Axum WebSocket handler:
async fn ws_handler(ws: WebSocketUpgrade) -> Response {
ws.on_upgrade(|socket| handle_socket(socket))
}
async fn handle_socket(mut socket: WebSocket) {
let udp = UdpSocket::bind("0.0.0.0:0").await.unwrap();
udp.join_multicast_v4("239.1.2.3".parse().unwrap(), Ipv4Addr::UNSPECIFIED).unwrap();
loop {
let mut buf = [0u8; 256];
let (len, _) = udp.recv_from(&mut buf).await.unwrap();
let frame = CanFrame::decode(&buf[..len]).unwrap();
let json = serde_json::to_string(&frame).unwrap();
socket.send(Message::Text(json)).await.unwrap();
}
}The bridge also accepts WebSocket messages from the dashboard (ground operator telecommands) and injects them into the UDP multicast group — completing the bidirectional command/telemetry loop.