#![cfg_attr(
feature = "async",
doc = r#"
## Async version
This library provides an ability for being executed in an asynchronous context using `tokio` as
the execution runtime.
Please note that the current async implementation is in beta, the interface can be object to
drastic changes.
The async `Client` and `ClientBuilder` support a similar interface to the sync version and live
in the [`asynchronous`] module. In order to enable the support, you need to enable the `async`
feature flag:
```toml
rust_socketio = { version = "^0.4.1", features = ["async"] }
```
The following code shows the example above in async fashion:
``` rust
use futures_util::FutureExt;
use rust_socketio::{
asynchronous::{Client, ClientBuilder},
Payload,
};
use serde_json::json;
use std::time::Duration;
#[tokio::main]
async fn main() {
// define a callback which is called when a payload is received
// this callback gets the payload as well as an instance of the
// socket to communicate with the server
let callback = |payload: Payload, socket: Client| {
async move {
match payload {
Payload::Text(values) => println!("Received: {:#?}", values),
Payload::Binary(bin_data) => println!("Received bytes: {:#?}", bin_data),
// This is deprecated use Payload::Text instead
Payload::String(str) => println!("Received: {}", str),
}
socket
.emit("test", json!({"got ack": true}))
.await
.expect("Server unreachable");
}
.boxed()
};
// get a socket that is connected to the admin namespace
let socket = ClientBuilder::new("http://localhost:4200/")
.namespace("/admin")
.on("test", callback)
.on("error", |err, _| {
async move { eprintln!("Error: {:#?}", err) }.boxed()
})
.connect()
.await
.expect("Connection failed");
// emit to the "foo" event
let json_payload = json!({"token": 123});
socket
.emit("foo", json_payload)
.await
.expect("Server unreachable");
// define a callback, that's executed when the ack got acked
let ack_callback = |message: Payload, _: Client| {
async move {
println!("Yehaa! My ack got acked?");
println!("Ack data: {:#?}", message);
}
.boxed()
};
let json_payload = json!({"myAckData": 123});
// emit with an ack
socket
.emit_with_ack("test", json_payload, Duration::from_secs(2), ack_callback)
.await
.expect("Server unreachable");
socket.disconnect().await.expect("Disconnect failed");
}
```"#
)]
#![allow(clippy::rc_buffer)]
#![warn(clippy::complexity)]
#![warn(clippy::style)]
#![warn(clippy::perf)]
#![warn(clippy::correctness)]
pub mod client;
pub mod event;
pub(crate) mod packet;
pub mod payload;
pub(self) mod socket;
pub mod error;
#[cfg(feature = "async")]
pub mod asynchronous;
pub use error::Error;
pub use {event::Event, payload::Payload};
pub use client::{ClientBuilder, RawClient, TransportType};
#[deprecated(since = "0.3.0-alpha-2", note = "Socket renamed to Client")]
pub use client::{ClientBuilder as SocketBuilder, RawClient as Socket};
#[cfg(test)]
pub(crate) mod test {
use url::Url;
const SERVER_URL: &str = "http://localhost:4200";
pub(crate) fn socket_io_server() -> Url {
let url = std::env::var("SOCKET_IO_SERVER").unwrap_or_else(|_| SERVER_URL.to_owned());
let mut url = Url::parse(&url).unwrap();
if url.path() == "/" {
url.set_path("/socket.io/");
}
url
}
const AUTH_SERVER_URL: &str = "http://localhost:4204";
pub(crate) fn socket_io_auth_server() -> Url {
let url =
std::env::var("SOCKET_IO_AUTH_SERVER").unwrap_or_else(|_| AUTH_SERVER_URL.to_owned());
let mut url = Url::parse(&url).unwrap();
if url.path() == "/" {
url.set_path("/socket.io/");
}
url
}
const RESTART_SERVER_URL: &str = "http://localhost:4205";
pub(crate) fn socket_io_restart_server() -> Url {
let url = std::env::var("SOCKET_IO_RESTART_SERVER")
.unwrap_or_else(|_| RESTART_SERVER_URL.to_owned());
let mut url = Url::parse(&url).unwrap();
if url.path() == "/" {
url.set_path("/socket.io/");
}
url
}
const RESTART_URL_AUTH_SERVER_URL: &str = "http://localhost:4206";
pub(crate) fn socket_io_restart_url_auth_server() -> Url {
let url = std::env::var("SOCKET_IO_RESTART_URL_AUTH_SERVER")
.unwrap_or_else(|_| RESTART_URL_AUTH_SERVER_URL.to_owned());
let mut url = Url::parse(&url).unwrap();
if url.path() == "/" {
url.set_path("/socket.io/");
}
url
}
}