Skip to main content

async_wsocket/
lib.rs

1// Copyright (c) 2022-2024 Yuki Kishimoto
2// Distributed under the MIT software license
3
4//! Async WebSocket
5
6#![forbid(unsafe_code)]
7#![warn(clippy::large_futures)]
8#![cfg_attr(feature = "default", doc = include_str!("../README.md"))]
9
10#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
11use std::net::SocketAddr;
12
13pub use futures_util;
14pub use url::{self, Url};
15
16pub mod message;
17#[cfg(not(target_arch = "wasm32"))]
18pub mod native;
19pub mod prelude;
20mod socket;
21#[cfg(target_arch = "wasm32")]
22pub mod wasm;
23
24pub use self::message::Message;
25#[cfg(not(target_arch = "wasm32"))]
26pub use self::native::Error;
27pub use self::socket::WebSocket;
28#[cfg(target_arch = "wasm32")]
29pub use self::wasm::Error;
30
31#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
32pub enum ConnectionMode {
33    /// Direct
34    #[default]
35    Direct,
36    /// Custom proxy
37    #[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
38    Proxy(SocketAddr),
39}
40
41impl ConnectionMode {
42    /// Direct connection
43    #[inline]
44    pub fn direct() -> Self {
45        Self::Direct
46    }
47
48    /// Proxy
49    #[inline]
50    #[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
51    pub fn proxy(addr: SocketAddr) -> Self {
52        Self::Proxy(addr)
53    }
54}
55
56/// Connect
57#[inline]
58pub async fn connect(url: &Url, mode: &ConnectionMode) -> Result<WebSocket, Error> {
59    WebSocket::connect(url, mode).await
60}