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
36pub use crate::protocol::engineio::{HandshakePacket, Packet, PacketId};
37pub use builder::ClientBuilder;
38pub use client::Client;
39pub use error::{Error, Result};
40pub use tls::TlsConfig;
41pub use transport::{Frame, Transport, TransportKind};
42
43/// The engine.io protocol revision this client speaks (`EIO=4`).
44pub const ENGINE_IO_VERSION: i32 = 4;
45
46/// Default bound for the handshake and the websocket connect (socket.io-client `timeout`, 20 s).
47pub const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20);
48
49#[cfg(test)]
50pub(crate) mod test {
51    use super::*;
52    use url::Url;
53
54    #[cfg(any(feature = "rustls", feature = "native-tls"))]
55    const CERT_PATH: &str = "../../ci/cert/ca.crt";
56
57    /// Trusts the test CA from `ci/cert` (`CA_CERT_PATH` overrides the path).
58    #[cfg(any(feature = "rustls", feature = "native-tls"))]
59    pub(crate) fn tls_config() -> Result<TlsConfig> {
60        let cert_path = std::env::var("CA_CERT_PATH").unwrap_or_else(|_| CERT_PATH.to_owned());
61        let pem = std::fs::read(&cert_path)
62            .map_err(|e| Error::InvalidTlsConfig(format!("{cert_path}: {e}")))?;
63        TlsConfig::with_root_certificates_pem(&pem)
64    }
65
66    fn url(var: &str, default: &str) -> Result<Url> {
67        Ok(Url::parse(
68            &std::env::var(var).unwrap_or_else(|_| default.to_owned()),
69        )?)
70    }
71
72    /// The engine.io echo server (`ci/engine-io.js`; in-process fake unless `ENGINE_IO_SERVER`)
73    pub(crate) fn engine_io_server() -> Result<Url> {
74        Ok(Url::parse(&socketio_next_testing::engine_io_server())?)
75    }
76
77    /// The engine.io server that refuses upgrades (`ci/engine-io-polling.js`; in-process fake
78    /// unless `ENGINE_IO_POLLING_SERVER`)
79    pub(crate) fn engine_io_polling_server() -> Result<Url> {
80        Ok(Url::parse(
81            &socketio_next_testing::engine_io_polling_server(),
82        )?)
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}