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
pub mod events;
pub mod futures;
use events::CloseEvent;
use gloo_utils::errors::JsError;
use std::fmt;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Message {
Text(String),
Bytes(Vec<u8>),
}
#[derive(Copy, Clone, Debug)]
pub enum State {
Connecting,
Open,
Closing,
Closed,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum WebSocketError {
ConnectionError,
ConnectionClose(CloseEvent),
MessageSendError(JsError),
}
impl fmt::Display for WebSocketError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WebSocketError::ConnectionError => write!(f, "WebSocket connection failed"),
WebSocketError::ConnectionClose(e) => write!(
f,
"WebSocket Closed: code: {}, reason: {}",
e.code, e.reason
),
WebSocketError::MessageSendError(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for WebSocketError {}