2021-09-24 16:02:17 +08:00
|
|
|
use crate::service::ws::WsClientData;
|
2021-09-25 21:47:02 +08:00
|
|
|
|
2021-11-19 14:38:11 +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-09-24 16:02:17 +08:00
|
|
|
fn receive_data(&self, client_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-09-23 19:59:58 +08:00
|
|
|
inner: HashMap<WsModule, BizHandler>,
|
2021-09-23 17:50:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WsBizHandlers {
|
2021-11-19 14:38:11 +08:00
|
|
|
pub fn new() -> Self { Self { inner: HashMap::new() } }
|
2021-09-23 17:50:28 +08:00
|
|
|
|
2021-11-19 14:38:11 +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-09-23 19:59:58 +08:00
|
|
|
pub fn get(&self, source: &WsModule) -> Option<BizHandler> {
|
2021-09-23 17:50:28 +08:00
|
|
|
match self.inner.get(source) {
|
|
|
|
None => None,
|
|
|
|
Some(handler) => Some(handler.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|