use std::sync::Arc; use topcoat::{ Result, context::{Cx, app_context}, router::{ error::RouterErrorExt, content::websocket::WebSocketUpgrade, path_param, response::Response, route, }, }; use super::NoteId; use crate::hub::{Hub, serve_socket}; use crate::store::Store; /// GET /notes/{note_id}/ws: live sync for one note. #[route(GET)] async fn websocket(cx: &Cx, upgrade: WebSocketUpgrade) -> Result { let store = app_context::>(cx).clone(); let hub = app_context::>(cx).clone(); let id = path_param::(cx).to_owned(); store.get_note(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?; let room = hub.room(&store, &id).await.map_err(topcoat::Error::from_anyhow)?; // One edit is one message, sent by browsers as one frame: as large as an import. let max = 128 << 20; upgrade.max_message_size(max).max_frame_size(max).on_upgrade(move |socket| async move { serve_socket(hub, store, room, socket).await; }) }