http_request/request/socket/websocket/trait.rs
1use crate::*;
2
3/// Synchronous WebSocket operations trait.
4///
5/// Defines the interface for synchronous WebSocket operations including:
6/// - Sending messages (text, binary, ping, pong)
7/// - Receiving messages
8/// - Closing connections
9/// - Checking connection status
10pub trait WebSocketTrait: Send + Sync {
11 /// Sends a text message synchronously.
12 ///
13 /// # Arguments
14 ///
15 /// - `&str` - The text message to send.
16 ///
17 /// # Returns
18 ///
19 /// - `WebSocketResult` - Result indicating success or failure.
20 fn send_text(&mut self, text: &str) -> WebSocketResult;
21 /// Sends a binary message synchronously.
22 ///
23 /// # Arguments
24 ///
25 /// - `&[u8]` - The binary data to send.
26 ///
27 /// # Returns
28 ///
29 /// - `WebSocketResult` - Result indicating success or failure.
30 fn send_binary(&mut self, data: &[u8]) -> WebSocketResult;
31 /// Sends a ping message synchronously.
32 ///
33 /// # Arguments
34 ///
35 /// - `&[u8]` - The ping data to send.
36 ///
37 /// # Returns
38 ///
39 /// - `WebSocketResult` - Result indicating success or failure.
40 fn send_ping(&mut self, data: &[u8]) -> WebSocketResult;
41 /// Sends a pong message synchronously.
42 ///
43 /// # Arguments
44 ///
45 /// - `&[u8]` - The pong data to send.
46 ///
47 /// # Returns
48 ///
49 /// - `WebSocketResult` - Result indicating success or failure.
50 fn send_pong(&mut self, data: &[u8]) -> WebSocketResult;
51 /// Receives a message synchronously.
52 ///
53 /// # Returns
54 ///
55 /// - `WebSocketMessageResult` - Result containing the received message or error.
56 fn receive(&mut self) -> WebSocketMessageResult;
57 /// Closes the WebSocket connection synchronously.
58 ///
59 /// # Returns
60 ///
61 /// - `WebSocketResult` - Result indicating success or failure.
62 fn close(&mut self) -> WebSocketResult;
63 /// Checks if the WebSocket is currently connected.
64 ///
65 /// # Returns
66 ///
67 /// - `bool` - True if connected, false otherwise.
68 fn is_connected(&self) -> bool;
69}
70
71/// Asynchronous WebSocket operations trait.
72///
73/// Defines the interface for asynchronous WebSocket operations including:
74/// - Sending messages (text, binary, ping, pong)
75/// - Receiving messages
76/// - Closing connections
77/// - Checking connection status
78pub trait AsyncWebSocketTrait: Send + Sync {
79 /// Sends a text message asynchronously.
80 ///
81 /// # Arguments
82 ///
83 /// - `&str` - The text message to send.
84 ///
85 /// # Returns
86 ///
87 /// - `WebSocketResult` - Result indicating success or failure.
88 fn send_text<'a>(
89 &'a mut self,
90 text: &'a str,
91 ) -> Pin<Box<dyn Future<Output = WebSocketResult> + Send + 'a>>;
92 /// Sends a binary message asynchronously.
93 ///
94 /// # Arguments
95 ///
96 /// - `&[u8]` - The binary data to send.
97 ///
98 /// # Returns
99 ///
100 /// - `WebSocketResult` - Result indicating success or failure.
101 fn send_binary<'a>(
102 &'a mut self,
103 data: &'a [u8],
104 ) -> Pin<Box<dyn Future<Output = WebSocketResult> + Send + 'a>>;
105 /// Sends a ping message asynchronously.
106 ///
107 /// # Arguments
108 ///
109 /// - `&[u8]` - The ping data to send.
110 ///
111 /// # Returns
112 ///
113 /// - `WebSocketResult` - Result indicating success or failure.
114 fn send_ping<'a>(
115 &'a mut self,
116 data: &'a [u8],
117 ) -> Pin<Box<dyn Future<Output = WebSocketResult> + Send + 'a>>;
118 /// Sends a pong message asynchronously.
119 ///
120 /// # Arguments
121 ///
122 /// - `&[u8]` - The pong data to send.
123 ///
124 /// # Returns
125 ///
126 /// - `WebSocketResult` - Result indicating success or failure.
127 fn send_pong<'a>(
128 &'a mut self,
129 data: &'a [u8],
130 ) -> Pin<Box<dyn Future<Output = WebSocketResult> + Send + 'a>>;
131 /// Receives a message asynchronously.
132 ///
133 /// # Returns
134 ///
135 /// - `WebSocketMessageResult` - Result containing the received message or error.
136 fn receive<'a>(
137 &'a mut self,
138 ) -> Pin<Box<dyn Future<Output = WebSocketMessageResult> + Send + 'a>>;
139 /// Closes the WebSocket connection asynchronously.
140 ///
141 /// # Returns
142 ///
143 /// - `WebSocketResult` - Result indicating success or failure.
144 fn close<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = WebSocketResult> + Send + 'a>>;
145 /// Checks if the WebSocket is currently connected.
146 ///
147 /// # Returns
148 ///
149 /// - `bool` - True if connected, false otherwise.
150 fn is_connected(&self) -> bool;
151}