api_sketching.rs

 1use bevy_app::{App, RunMode, ScheduleRunnerPlugin};
 2use bevy_ecs::message::{Message, MessageReader};
 3use bevy_tokio_tasks::TokioTasksPlugin;
 4use tokio_xmpp::Stanza;
 5use xmpp::component::resource::{ComponentConfig, XmppComponent};
 6use xmpp::component::{StanzaMatcher, StanzaMatcherPlugin};
 7
 8#[derive(Message)]
 9struct TestMessage {
10    content: String,
11}
12
13#[derive(Clone)]
14struct TestMatcher;
15
16impl StanzaMatcher for TestMatcher {
17    type Message = TestMessage;
18
19    fn matches(&mut self, candidate: &Stanza) -> Option<Self::Message> {
20        match candidate {
21            Stanza::Message(msg) => {
22                let body = msg.bodies.get("")?.clone();
23                Some(TestMessage { content: body })
24            }
25            _ => None,
26        }
27    }
28}
29
30fn log_test_messages(mut messages: MessageReader<TestMessage>) {
31    for msg in messages.read() {
32        println!("Received message: {}", msg.content);
33    }
34}
35
36fn main() {
37    println!("finished connecting");
38
39    App::new()
40        .add_plugins(ScheduleRunnerPlugin {
41            run_mode: RunMode::Loop { wait: None },
42        })
43        .add_plugins(TokioTasksPlugin::default())
44        .add_plugins(StanzaMatcherPlugin::new().matcher(TestMatcher))
45        .init_resource::<ComponentConfig>()
46        .init_resource::<XmppComponent>()
47        .add_systems(bevy_app::Update, log_test_messages)
48        .run();
49}