tokio_websocket_client/message.rs
1#[allow(missing_docs)]
2pub enum Message {
3 Text(String),
4 Binary(Vec<u8>),
5 Ping(Vec<u8>),
6 Pong(Vec<u8>),
7 Close(CloseCode, String),
8}
9
10#[allow(missing_docs)]
11#[derive(Debug, Clone)]
12pub enum CloseCode {
13 /// Indicates a normal closure, meaning that the purpose for
14 /// which the connection was established has been fulfilled.
15 Normal,
16 /// Indicates that an endpoint is "going away", such as a server
17 /// going down or a browser having navigated away from a page.
18 Away,
19 /// Indicates that an endpoint is terminating the connection due
20 /// to a protocol error.
21 Protocol,
22 /// Indicates that an endpoint is terminating the connection
23 /// because it has received a type of data it cannot accept (e.g., an
24 /// endpoint that understands only text data MAY send this if it
25 /// receives a binary message).
26 Unsupported,
27 /// Indicates that no status code was included in a closing frame. This
28 /// close code makes it possible to use a single method, `on_close` to
29 /// handle even cases where no close code was provided.
30 Status,
31 /// Indicates an abnormal closure. If the abnormal closure was due to an
32 /// error, this close code will not be used. Instead, the `on_error` method
33 /// of the handler will be called with the error. However, if the connection
34 /// is simply dropped, without an error, this close code will be sent to the
35 /// handler.
36 Abnormal,
37 /// Indicates that an endpoint is terminating the connection
38 /// because it has received data within a message that was not
39 /// consistent with the type of the message (e.g., non-UTF-8 \[RFC3629\]
40 /// data within a text message).
41 Invalid,
42 /// Indicates that an endpoint is terminating the connection
43 /// because it has received a message that violates its policy. This
44 /// is a generic status code that can be returned when there is no
45 /// other more suitable status code (e.g., Unsupported or Size) or if there
46 /// is a need to hide specific details about the policy.
47 Policy,
48 /// Indicates that an endpoint is terminating the connection
49 /// because it has received a message that is too big for it to
50 /// process.
51 Size,
52 /// Indicates that an endpoint (client) is terminating the
53 /// connection because it has expected the server to negotiate one or
54 /// more extension, but the server didn't return them in the response
55 /// message of the WebSocket handshake. The list of extensions that
56 /// are needed should be given as the reason for closing.
57 /// Note that this status code is not used by the server, because it
58 /// can fail the WebSocket handshake instead.
59 Extension,
60 /// Indicates that a server is terminating the connection because
61 /// it encountered an unexpected condition that prevented it from
62 /// fulfilling the request.
63 Error,
64 /// Indicates that the server is restarting. A client may choose to reconnect,
65 /// and if it does, it should use a randomized delay of 5-30 seconds between attempts.
66 Restart,
67 /// Indicates that the server is overloaded and the client should either connect
68 /// to a different IP (when multiple targets exist), or reconnect to the same IP
69 /// when a user has performed an action.
70 Again,
71 #[doc(hidden)]
72 Tls,
73 #[doc(hidden)]
74 Reserved(u16),
75 #[doc(hidden)]
76 Iana(u16),
77 #[doc(hidden)]
78 Private(u16),
79 #[doc(hidden)]
80 Bad(u16),
81}
82
83impl From<CloseCode> for u16 {
84 fn from(code: CloseCode) -> Self {
85 Self::from(&code)
86 }
87}
88
89impl From<&CloseCode> for u16 {
90 fn from(code: &CloseCode) -> Self {
91 match code {
92 CloseCode::Normal => 1000,
93 CloseCode::Away => 1001,
94 CloseCode::Protocol => 1002,
95 CloseCode::Unsupported => 1003,
96 CloseCode::Status => 1005,
97 CloseCode::Abnormal => 1006,
98 CloseCode::Invalid => 1007,
99 CloseCode::Policy => 1008,
100 CloseCode::Size => 1009,
101 CloseCode::Extension => 1010,
102 CloseCode::Error => 1011,
103 CloseCode::Restart => 1012,
104 CloseCode::Again => 1013,
105 CloseCode::Tls => 1015,
106 CloseCode::Reserved(code)
107 | CloseCode::Private(code)
108 | CloseCode::Iana(code)
109 | CloseCode::Bad(code) => *code,
110 }
111 }
112}
113
114impl From<u16> for CloseCode {
115 fn from(code: u16) -> Self {
116 match code {
117 1000 => Self::Normal,
118 1001 => Self::Away,
119 1002 => Self::Protocol,
120 1003 => Self::Unsupported,
121 1005 => Self::Status,
122 1006 => Self::Abnormal,
123 1007 => Self::Invalid,
124 1008 => Self::Policy,
125 1009 => Self::Size,
126 1010 => Self::Extension,
127 1011 => Self::Error,
128 1012 => Self::Restart,
129 1013 => Self::Again,
130 1015 => Self::Tls,
131 1016..=2999 => Self::Reserved(code),
132 3000..=3999 => Self::Iana(code),
133 4000..=4999 => Self::Private(code),
134 _ => Self::Bad(code),
135 }
136 }
137}