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
//!
//!# Very Simple Websocket For Rust
//!
//!`ws2` is a very easy to use WebSocket server & client for Rust, builds on `ws` crate. it was designed for production, the receive timeout provides a non-blocking way.
//!
//!## Server Example
//!
//!```rust
//!use log2::*;
//!use ws2::{Pod, WebSocket};
//!
//!struct Worker;
//!
//!impl ws2::Handler for Worker {
//! fn on_open(&mut self, ws: &WebSocket) -> Pod {
//! info!("on open: {ws}");
//! Ok(())
//! }
//!
//! fn on_close(&mut self, ws: &WebSocket) -> Pod {
//! info!("on close: {ws}");
//! Ok(())
//! }
//!
//! fn on_message(&mut self, ws: &WebSocket, msg: String) -> Pod {
//! info!("on message: {msg}, {ws}");
//! let echo = format!("echo: {msg}");
//! let n = ws.send(echo);
//! Ok(n?)
//! }
//!}
//!
//!fn main() -> Pod {
//! let _log2 = log2::start();
//! let address = "127.0.0.1:3125";
//! let mut worker = Worker {};
//!
//! info!("listen on: {address}");
//! let mut server = ws2::listen(address)?;
//!
//! loop {
//! let _ = server.process(&mut worker, 0.5);
//! // do other stuff
//! }
//!}
//!
//!```
//!
//!## Client Example
//!
//!The websocket client was designed as out-of-the-box, it will auto reconnect every 3s.
//!
//!```rust
//!use log2::*;
//!use ws2::{Pod, WebSocket};
//!
//!struct Worker;
//!
//!impl ws2::Handler for Worker {
//! fn on_open(&mut self, ws: &WebSocket) -> Pod {
//! // ws.send("Hello World")?;
//! info!("on open: {ws}");
//! Ok(())
//! }
//!
//! fn on_close(&mut self, ws: &WebSocket) -> Pod {
//! info!("on close: {ws}");
//! Ok(())
//! }
//!
//! fn on_message(&mut self, ws: &WebSocket, msg: String) -> Pod {
//! info!("on message: {msg}, {ws}");
//! Ok(())
//! }
//!}
//!
//!fn main() -> Pod {
//! let _log2 = log2::start();
//! let url = "wss://stream.binance.com:9443/ws/btcusdt@miniTicker";
//! let mut client = ws2::connect(url);
//! let mut workder = Worker {};
//!
//! loop {
//! let _ = client.process(&mut workder, 0.5);
//! // do other stuff
//! }
//!}
//!```
//!
use Display;
use ;
/// Flexible result
pub type Pod = Result;
/// Wait infinitely
pub const INFINITE: f32 = MAX;
pub use connect;
pub use Client;
pub use listen;
pub use Server;
pub use Sender;
/// WebSocket sender