http_request/request/socket/websocket/
struct.rs1use crate::*;
2
3#[derive(Debug)]
8pub enum WebSocketConnectionType {
9 Direct(WebSocketStream<MaybeTlsStream<AsyncTcpStream>>),
10 Proxy(WebSocketStream<WebSocketProxyTunnelStream>),
11}
12
13#[derive(Debug)]
20pub struct WebSocket {
21 pub(crate) url: Arc<String>,
23 pub(crate) header: Arc<RequestHeaders>,
25 pub(crate) config: ArcRwLock<WebSocketConfig>,
27 pub(crate) connected: Arc<AtomicBool>,
29 pub(crate) connection: WebSocketConnection,
31}
32
33impl Clone for WebSocket {
39 fn clone(&self) -> Self {
40 Self {
41 url: self.url.clone(),
42 header: self.header.clone(),
43 config: self.config.clone(),
44 connected: Arc::new(AtomicBool::new(false)),
45 connection: Arc::new(AsyncMutex::new(None)),
46 }
47 }
48}
49
50impl Default for WebSocket {
59 #[inline(always)]
60 fn default() -> Self {
61 Self {
62 url: Arc::new(String::new()),
63 header: Arc::new(hash_map_xx_hash3_64()),
64 config: Arc::new(RwLock::new(WebSocketConfig::default())),
65 connected: Arc::new(AtomicBool::new(false)),
66 connection: Arc::new(AsyncMutex::new(None)),
67 }
68 }
69}
70
71impl Stream for WebSocketConnectionType {
76 type Item =
77 Result<tokio_tungstenite::tungstenite::Message, tokio_tungstenite::tungstenite::Error>;
78
79 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
80 match &mut *self {
81 WebSocketConnectionType::Direct(stream) => Pin::new(stream).poll_next(cx),
82 WebSocketConnectionType::Proxy(stream) => Pin::new(stream).poll_next(cx),
83 }
84 }
85}
86
87impl Sink<tokio_tungstenite::tungstenite::Message> for WebSocketConnectionType {
92 type Error = tokio_tungstenite::tungstenite::Error;
93
94 fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
95 match &mut *self {
96 WebSocketConnectionType::Direct(stream) => Pin::new(stream).poll_ready(cx),
97 WebSocketConnectionType::Proxy(stream) => Pin::new(stream).poll_ready(cx),
98 }
99 }
100
101 fn start_send(
102 mut self: Pin<&mut Self>,
103 item: tokio_tungstenite::tungstenite::Message,
104 ) -> Result<(), Self::Error> {
105 match &mut *self {
106 WebSocketConnectionType::Direct(stream) => Pin::new(stream).start_send(item),
107 WebSocketConnectionType::Proxy(stream) => Pin::new(stream).start_send(item),
108 }
109 }
110
111 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
112 match &mut *self {
113 WebSocketConnectionType::Direct(stream) => Pin::new(stream).poll_flush(cx),
114 WebSocketConnectionType::Proxy(stream) => Pin::new(stream).poll_flush(cx),
115 }
116 }
117
118 fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
119 match &mut *self {
120 WebSocketConnectionType::Direct(stream) => Pin::new(stream).poll_close(cx),
121 WebSocketConnectionType::Proxy(stream) => Pin::new(stream).poll_close(cx),
122 }
123 }
124}