Skip to content

Commit 78ced5a

Browse files
author
Itsusinn逸新
committed
refactor: more complex example
1 parent 31985bb commit 78ced5a

File tree

1 file changed

+78
-9
lines changed

1 file changed

+78
-9
lines changed

examples/app_ws.rs

+78-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,91 @@
1+
use color_eyre::eyre::Result;
12
use std::sync::Arc;
23
use walle_core::action::Action;
3-
use walle_core::alt::TracingHandler;
4-
use walle_core::config::AppConfig;
4+
use walle_core::alt::{ColoredAlt, TracingHandler};
5+
use walle_core::config::{AppConfig, WebSocketServer};
56
use walle_core::event::Event;
67
use walle_core::obc::AppOBC;
78
use walle_core::resp::Resp;
8-
use walle_core::OneBot;
9+
use walle_core::WalleResult;
10+
use walle_core::{ActionHandler, EventHandler, OneBot};
11+
use tokio::task::JoinHandle;
912

1013
#[tokio::main]
11-
async fn main() {
14+
async fn main() -> Result<()> {
1215
color_eyre::install()?;
1316
init_log();
14-
AppOBC::new(),
15-
TracingHandler::<Event, Action, Resp>::default(),
16-
));
17-
ob.start(AppConfig::default(), (), true).await.unwrap();
18-
// ob.wait_all().await;
17+
let ob = Arc::new(OneBot::new(AppOBC::new(), MyEventHandler));
18+
let mut config = AppConfig::default();
19+
config.websocket_rev.clear();
20+
let ws_server = WebSocketServer {
21+
host: std::net::IpAddr::from([0, 0, 0, 0]),
22+
port: 2456,
23+
access_token: None,
24+
};
25+
config.websocket_rev.push(ws_server);
26+
ob.start(config, (), true).await.unwrap();
27+
tokio::spawn(async {
28+
let ob = ob;
29+
ob.handle_action(action)
30+
});
31+
ob.wait_all().await;
1932
ob.shutdown(true).await.ok();
33+
Ok(())
34+
}
35+
#[derive(Debug)]
36+
struct MyEventHandler;
37+
impl EventHandler<Event, Action, Resp> for MyEventHandler {
38+
type Config = ();
39+
async fn start<AH, EH>(
40+
&self,
41+
_ob: &Arc<OneBot<AH, EH>>,
42+
_config: Self::Config,
43+
) -> WalleResult<Vec<JoinHandle<()>>>
44+
where
45+
AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static,
46+
EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static,
47+
{
48+
Ok(vec![])
49+
}
50+
async fn call<AH, EH>(
51+
&self,
52+
event: Event,
53+
_ob: &Arc<OneBot<AH, EH>>,
54+
) -> walle_core::WalleResult<()>
55+
where
56+
AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static,
57+
EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static,
58+
{
59+
tracing::info!("{}", event.colored_alt());
60+
Ok(())
61+
}
62+
async fn shutdown(&self) {
63+
tracing::info!("Shutting down TracingHandler")
64+
}
65+
66+
fn on_onebot_connect<AH, EH>(
67+
&self,
68+
_ob: &Arc<OneBot<AH, EH>>,
69+
) -> impl futures_util::Future<Output = WalleResult<()>>
70+
where
71+
AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static,
72+
EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static,
73+
{
74+
async { Ok(()) }
75+
}
76+
77+
fn on_onebot_disconnect<AH, EH>(
78+
&self,
79+
_ob: &Arc<OneBot<AH, EH>>,
80+
) -> impl futures_util::Future<Output = WalleResult<()>>
81+
where
82+
AH: ActionHandler<Event, Action, Resp> + Send + Sync + 'static,
83+
EH: EventHandler<Event, Action, Resp> + Send + Sync + 'static,
84+
{
85+
async { Ok(()) }
86+
}
87+
}
88+
2089
fn init_log() {
2190
use tracing::Level;
2291
use tracing_subscriber::prelude::*;

0 commit comments

Comments
 (0)