use std::sync::Arc;
pub struct Connect<'a> {
pub conn_id: &'a Arc<String>,
pub user_id: Option<&'a str>,
pub cookies: &'a str,
pub(crate) state: &'a super::state::SharedState,
pub(crate) handle: &'a super::state::ConnectionHandle,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Inbound {
Json(serde_json::Value),
Text(String),
Binary(Vec<u8>),
}
pub trait Handler: Send + Sync + 'static {
type Connection: Send + 'static;
fn connect(&self, connect: &Connect<'_>) -> Option<Self::Connection>;
fn message(&self, conn: &mut Self::Connection, message: Inbound, size: usize) -> bool;
fn signed(&self, conn: &mut Self::Connection, message: serde_json::Value, size: usize) -> bool;
fn disconnect(&self, conn: Self::Connection);
fn presence(&self, event: Presence) {
let _ = event;
}
}
pub(crate) struct Panicked;
pub(crate) fn guarded<R>(f: impl FnOnce() -> R) -> Result<R, Panicked> {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).map_err(|_| Panicked)
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Presence {
Join {
topic: String,
user_id: String,
data: serde_json::Value,
},
Leave {
topic: String,
user_id: String,
data: serde_json::Value,
},
}