2021-12-06 21:47:21 +08:00
|
|
|
use crate::web_socket::WsClientData;
|
2021-12-16 22:24:05 +08:00
|
|
|
use lib_ws::WSModule;
|
2021-09-23 19:59:58 +08:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2021-09-23 17:50:28 +08:00
|
|
|
|
|
|
|
pub trait WsBizHandler: Send + Sync {
|
2021-12-11 17:48:39 +08:00
|
|
|
fn receive(&self, data: WsClientData);
|
2021-09-23 17:50:28 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 19:59:58 +08:00
|
|
|
pub type BizHandler = Arc<dyn WsBizHandler>;
|
2021-09-23 17:50:28 +08:00
|
|
|
pub struct WsBizHandlers {
|
2021-12-16 22:24:05 +08:00
|
|
|
inner: HashMap<WSModule, BizHandler>,
|
2021-09-23 17:50:28 +08:00
|
|
|
}
|
|
|
|
|
2021-12-02 21:54:21 +08:00
|
|
|
impl std::default::Default for WsBizHandlers {
|
|
|
|
fn default() -> Self { Self { inner: HashMap::new() } }
|
|
|
|
}
|
|
|
|
|
2021-09-23 17:50:28 +08:00
|
|
|
impl WsBizHandlers {
|
2021-12-02 21:54:21 +08:00
|
|
|
pub fn new() -> Self { WsBizHandlers::default() }
|
2021-09-23 17:50:28 +08:00
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
pub fn register(&mut self, source: WSModule, handler: BizHandler) { self.inner.insert(source, handler); }
|
2021-09-23 17:50:28 +08:00
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
pub fn get(&self, source: &WSModule) -> Option<BizHandler> { self.inner.get(source).cloned() }
|
2021-09-23 17:50:28 +08:00
|
|
|
}
|