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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Core event and error types used by Wynd.
//!
//! Defines strongly-typed events for text, binary, and close messages, and
//! the `WyndError` type for server-level errors. These types are consumed by
//! public APIs in the `conn`, `handle`, and `wynd` modules.
use ;
/// Represents a text message event received from a WebSocket client.
///
/// This event is triggered when a text message is received from the client.
/// The message data is guaranteed to be valid UTF-8.
///
/// ## Fields
///
/// - `data`: The UTF-8 text content of the message
///
/// ## Example
///
/// ```rust
/// use wynd::types::TextMessageEvent;
/// use wynd::wynd::{Wynd, Standalone};
///
/// #[tokio::main]
/// async fn main() {
/// let mut wynd: Wynd<Standalone> = Wynd::new();
///
/// wynd.on_connection(|conn| async move {
/// conn.on_text(|event, handle| async move {
/// println!("Received text message: {}", event.data);
///
/// // Echo the message back
/// let _ = handle.send_text(&format!("Echo: {}", event.data)).await;
/// });
/// });
///
/// wynd.listen(8080, || {
/// println!("Server listening on port 8080");
/// });
/// }
/// ```
/// Represents a binary message event received from a WebSocket client.
///
/// This event is triggered when binary data is received from the client.
/// The data can contain any sequence of bytes.
///
/// ## Fields
///
/// - `data`: The binary data as a vector of bytes
///
/// ## Example
///
/// ```rust
/// use wynd::types::BinaryMessageEvent;
/// use wynd::wynd::{Wynd, Standalone};
///
/// #[tokio::main]
/// async fn main() {
/// let mut wynd: Wynd<Standalone> = Wynd::new();
///
/// wynd.on_connection(|conn| async move {
/// conn.on_binary(|event, handle| async move {
/// println!("Received binary data: {} bytes", event.data.len());
///
/// // Process the data before moving it
/// if event.data.len() > 1024 {
/// let _ = handle.send_text("Data too large").await;
/// } else {
/// // Echo the binary data back
/// let _ = handle.send_binary(event.data).await;
/// }
/// });
/// });
///
/// wynd.listen(8080, || {
/// println!("Server listening on port 8080");
/// });
/// }
/// ```
/// Represents a WebSocket connection close event.
///
/// This event is triggered when a WebSocket connection is closed,
/// either by the client or due to an error. It contains information
/// about the reason for the closure.
///
/// ## Fields
///
/// - `code`: The WebSocket close code indicating the reason for closure
/// - `reason`: A human-readable description of the closure reason
///
/// ## Close Codes
///
/// Common WebSocket close codes:
/// - `1000`: Normal closure
/// - `1001`: Going away (client leaving)
/// - `1002`: Protocol error
/// - `1003`: Unsupported data type
/// - `1006`: Abnormal closure
/// - `1009`: Message too large
/// - `1011`: Internal server error
///
/// ## Example
///
/// ```rust
/// use wynd::types::CloseEvent;
/// use wynd::wynd::{Wynd, Standalone};
///
/// #[tokio::main]
/// async fn main() {
/// let mut wynd: Wynd<Standalone> = Wynd::new();
///
/// wynd.on_connection(|conn| async move {
/// conn.on_close(|event| async move {
/// println!("Connection closed: code={}, reason={}", event.code, event.reason);
///
/// match event.code {
/// 1000 => println!("Normal closure"),
/// 1001 => println!("Client going away"),
/// 1002 => println!("Protocol error"),
/// 1006 => println!("Abnormal closure"),
/// _ => println!("Other closure: {}", event.code),
/// }
/// });
/// });
///
/// wynd.listen(8080, || {
/// println!("Server listening on port 8080");
/// });
/// }
/// ```
// /// Represents a WebSocket error event.
// ///
// /// This event is triggered when an error occurs during WebSocket
// /// communication. It contains information about the error that occurred.
// ///
// /// ## Fields
// ///
// /// - `message`: A description of the error that occurred
// ///
// /// ## Example
// ///
// /// ```rust
// /// use wynd::types::ErrorEvent;
// /// use wynd::wynd::Wynd;
// ///
// /// #[tokio::main]
// /// async fn main() {
// /// let mut wynd = Wynd::new();
// ///
// /// wynd.on_connection(|conn| async move {
// /// conn.on_error(|event| async move {
// /// eprintln!("WebSocket error: {}", event.message);
// ///
// /// // Log the error or take corrective action
// /// if event.message.contains("timeout") {
// /// println!("Connection timed out, will retry");
// /// }
// /// });
// /// });
// ///
// /// wynd.listen(8080, || {
// /// println!("Server listening on port 8080");
// /// })
// /// .await
// /// .unwrap();
// /// }
// /// ```
// pub struct ErrorEvent {
// /// A description of the error that occurred.
// pub message: String,
// }
// impl Default for ErrorEvent {
// /// Creates a default error event with empty message.
// fn default() -> Self {
// Self::new(String::new())
// }
// }
// impl ErrorEvent {
// /// Creates a new error event.
// ///
// /// ## Parameters
// ///
// /// - `message`: The error description
// ///
// /// ## Returns
// ///
// /// Returns a new `ErrorEvent` with the provided message.
// pub(crate) fn new<T: Into<String>>(message: T) -> Self {
// Self {
// message: message.into(),
// }
// }
// }
/// Represents a Wynd server error.
///
/// This type is used to represent errors that occur at the server level,
/// such as connection acceptance failures, WebSocket handshake errors,
/// or other server-related issues.
///
/// ## Example
///
/// ```rust
/// use wynd::types::WyndError;
/// use wynd::wynd::{Wynd, Standalone};
///
/// #[tokio::main]
/// async fn main() {
/// let mut wynd: Wynd<Standalone> = Wynd::new();
///
/// // Handle server-level errors
/// wynd.on_error(|err| async move {
/// eprintln!("Server error: {}", err);
///
/// // Log the error or take corrective action
/// if err.to_string().contains("address already in use") {
/// eprintln!("Port is already in use, try a different port");
/// }
/// });
///
/// wynd.listen(8080, || {
/// println!("Server listening on port 8080");
/// });
/// }
/// ```