Skip to main content

tf_rust_socketio/asynchronous/
mod.rs

1mod client;
2mod generator;
3mod socket;
4
5#[cfg(feature = "async")]
6pub use client::builder::ClientBuilder;
7pub use client::client::{Client, ReconnectSettings};
8
9// re-export the macro
10pub use crate::{async_any_callback, async_callback};
11
12#[doc = r#"
13A macro to wrap an async callback function to be used in the client.
14
15This macro is used to wrap a callback function that can handle a specific event.
16
17```rust
18use tf_rust_socketio::async_callback;
19use tf_rust_socketio::asynchronous::{Client, ClientBuilder};
20use tf_rust_socketio::{Event, Payload};
21
22pub async fn callback(payload: Payload, client: Client) {}
23
24#[tokio::main]
25async fn main() {
26    let socket = ClientBuilder::new("http://example.com")
27            .on("message", async_callback!(callback))
28            .connect()
29            .await;
30}
31```
32"#]
33#[macro_export]
34macro_rules! async_callback {
35    ($f:expr) => {{
36        use futures_util::FutureExt;
37        |payload: Payload, client: Client| $f(payload, client).boxed()
38    }};
39}
40
41#[doc = r#"
42A macro to wrap an async callback function to be used in the client.
43
44This macro is used to wrap a callback function that can handle any event.
45
46```rust
47use tf_rust_socketio::async_any_callback;
48use tf_rust_socketio::asynchronous::{Client, ClientBuilder};
49use tf_rust_socketio::{Event, Payload};
50
51pub async fn callback_any(event: Event, payload: Payload, client: Client) {}
52
53#[tokio::main]
54async fn main() {
55    let socket = ClientBuilder::new("http://example.com")
56            .on_any(async_any_callback!(callback_any))
57            .connect()
58            .await;
59}
60```
61"#]
62#[macro_export]
63macro_rules! async_any_callback {
64    ($f:expr) => {{
65        use futures_util::FutureExt;
66        |event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed()
67    }};
68}