1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
pub mod error;
pub mod jaconfig;
pub mod jaconnection;
pub mod jahandle;
pub mod japlugin;
pub mod japrotocol;
pub mod jasession;
pub mod jatask;
pub mod prelude;
pub mod transport;

mod demuxer;
mod jarouter;
mod tmanager;

use crate::transport::trans::Transport;
use jaconfig::JaConfig;
use jaconfig::TransportType;
use jaconnection::JaConnection;
use prelude::JaResult;
use tracing::Level;

/// Creates a new connection with janus server from the provided configs
///
/// ## Example:
///
/// ```rust
/// let mut connection = jarust::connect(
///     JaConfig::new("ws://localhost:8188/ws", None, "janus"),
///     TransportType::Ws,
/// )
/// .await
/// .unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
pub async fn connect(jaconfig: JaConfig, transport_type: TransportType) -> JaResult<JaConnection> {
    let transport = match transport_type {
        jaconfig::TransportType::Ws => {
            transport::web_socket::WebsocketTransport::create_transport()
        }
    };
    connect_with_transport(jaconfig, transport).await
}

#[cfg(target_family = "wasm")]
/// Creates a new connection with janus server from the provided configs
pub async fn connect(jaconfig: JaConfig, transport_type: TransportType) -> JaResult<JaConnection> {
    let transport = transport::wasm_web_socket::WasmWsTransport;
    connect_with_transport(jaconfig, transport).await
}

/// Creates a new connection with janus server from the provided configs and custom transport.
///
/// Same as [`connect`], but takes a struct that implements [`Transport`] to be used instead
/// of using one of the predefined transport types [`TransportType`]
#[tracing::instrument(level = Level::TRACE)]
pub async fn connect_with_transport(
    jaconfig: JaConfig,
    transport: impl Transport,
) -> JaResult<JaConnection> {
    tracing::info!("Creating new connection");
    JaConnection::open(jaconfig, transport).await
}