Skip to main content

socketio_next/engineio/
mod.rs

1//! An async [Engine.IO v4](https://github.com/socketio/engine.io-protocol) client: HTTP long
2//! polling and websocket transports, the upgrade probe, the heartbeat, and TLS through
3//! rustls (default) or native-tls.
4//!
5//! This is the transport layer of the socket.io client; use it directly to talk raw engine.io.
6//!
7//! ```no_run
8//! use socketio_next::engineio::{ClientBuilder, Packet, PacketId};
9//! use futures_util::StreamExt;
10//! use url::Url;
11//!
12//! # async fn run() -> socketio_next::engineio::Result<()> {
13//! let mut client = ClientBuilder::new(Url::parse("http://localhost:4201")?)
14//!     .build()
15//!     .await?;
16//! client.connect().await?;
17//! client.emit(Packet::new(PacketId::Message, "hello")).await?;
18//! while let Some(packet) = client.next().await {
19//!     println!("{:?}", packet?);
20//! }
21//! # Ok(()) }
22//! ```
23
24mod builder;
25mod callback;
26mod client;
27mod error;
28mod generator;
29pub(crate) mod http;
30mod socket;
31pub mod tls;
32mod transport;
33pub mod transports;
34mod util;
35
36#[cfg(test)]
37mod js_parity;
38
39pub use crate::protocol::engineio::{HandshakePacket, Packet, PacketId};
40pub use builder::ClientBuilder;
41pub use client::Client;
42pub use error::{Error, Result};
43pub use tls::TlsConfig;
44pub use transport::{Frame, Transport, TransportKind};
45
46/// The engine.io protocol revision this client speaks (`EIO=4`).
47pub const ENGINE_IO_VERSION: i32 = 4;
48
49/// Default bound for the handshake and the websocket connect (socket.io-client `timeout`, 20 s).
50pub const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20);
51
52#[cfg(test)]
53pub(crate) mod test {
54    use super::*;
55    use url::Url;
56
57    #[cfg(any(feature = "rustls", feature = "native-tls"))]
58    const CERT_PATH: &str = "../../ci/cert/ca.crt";
59
60    /// Trusts the test CA from `ci/cert` (`CA_CERT_PATH` overrides the path).
61    #[cfg(any(feature = "rustls", feature = "native-tls"))]
62    pub(crate) fn tls_config() -> Result<TlsConfig> {
63        let cert_path = std::env::var("CA_CERT_PATH").unwrap_or_else(|_| CERT_PATH.to_owned());
64        let pem = std::fs::read(&cert_path)
65            .map_err(|e| Error::InvalidTlsConfig(format!("{cert_path}: {e}")))?;
66        TlsConfig::with_root_certificates_pem(&pem)
67    }
68
69    fn url(var: &str, default: &str) -> Result<Url> {
70        Ok(Url::parse(
71            &std::env::var(var).unwrap_or_else(|_| default.to_owned()),
72        )?)
73    }
74
75    /// The `engine.io` server for testing runs on port 4201
76    pub(crate) fn engine_io_server() -> Result<Url> {
77        url("ENGINE_IO_SERVER", "http://localhost:4201")
78    }
79
80    /// The `engine.io` server that refuses upgrades runs on port 4203
81    pub(crate) fn engine_io_polling_server() -> Result<Url> {
82        url("ENGINE_IO_POLLING_SERVER", "http://localhost:4203")
83    }
84
85    #[allow(dead_code)]
86    pub(crate) fn engine_io_server_secure() -> Result<Url> {
87        url("ENGINE_IO_SECURE_SERVER", "https://localhost:4202")
88    }
89}