forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

ws.rs32 lines, 1.0 KiBHistoryRawDownload
1use std::sync::Arc;
2
3use topcoat::{
4 Result,
5 context::{Cx, app_context},
6 router::{
7 error::RouterErrorExt,
8 content::websocket::WebSocketUpgrade,
9 path_param,
10 response::Response,
11 route,
12 },
13};
14
15use super::NoteId;
16use crate::hub::{Hub, serve_socket};
17use crate::store::Store;
18
19/// GET /notes/{note_id}/ws: live sync for one note.
20#[route(GET)]
21async fn websocket(cx: &Cx, upgrade: WebSocketUpgrade) -> Result<Response> {
22 let store = app_context::<Arc<Store>>(cx).clone();
23 let hub = app_context::<Arc<Hub>>(cx).clone();
24 let id = path_param::<NoteId>(cx).to_owned();
25 store.get_note(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?;
26 let room = hub.room(&store, &id).await.map_err(topcoat::Error::from_anyhow)?;
27 // One edit is one message, sent by browsers as one frame: as large as an import.
28 let max = 128 << 20;
29 upgrade.max_message_size(max).max_frame_size(max).on_upgrade(move |socket| async move {
30 serve_socket(hub, store, room, socket).await;
31 })
32}