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
//! ### Extractors for [`ConnectHandler`], [`ConnectMiddleware`], [`MessageHandler`] and [`DisconnectHandler`](crate::handler::DisconnectHandler).
//!
//! They can be used to extract data from the context of the handler and get specific params. Here are some examples of extractors:
//! * [`Data`]: extracts and deserialize from any receieved data, if a deserialization error occurs the handler won't be called:
//! - for [`ConnectHandler`]: extracts and deserialize from the incoming auth data
//! - for [`ConnectMiddleware`]: extract and deserialize from the incoming auth data.
//! In case of error, the middleware chain stops and a `connect_error` event is sent.
//! - for [`MessageHandler`]: extracts and deserialize from the incoming message data
//! * [`TryData`]: extracts and deserialize from the any received data but with a `Result` type in case of error:
//! - for [`ConnectHandler`] and [`ConnectMiddleware`]: extracts and deserialize from the incoming auth data
//! - for [`MessageHandler`]: extracts and deserialize from the incoming message data
//! * [`Event`]: extracts the message event name.
//! * [`SocketRef`]: extracts a reference to the [`Socket`](crate::socket::Socket).
//! * [`SocketIo`](crate::SocketIo): extracts a reference to the whole socket.io server context.
//! * [`AckSender`]: Can be used to send an ack response to the current message event.
//! * [`ProtocolVersion`](crate::ProtocolVersion): extracts the protocol version.
//! * [`TransportType`](crate::TransportType): extracts the transport type.
//! * [`DisconnectReason`](crate::socket::DisconnectReason): extracts the reason of the disconnection.
//! * [`State`]: extracts a [`Clone`] of a state previously set with [`SocketIoBuilder::with_state`](crate::io::SocketIoBuilder).
//! * [`Extension`]: extracts an extension of the given type stored on the called socket by cloning it.
//! * [`MaybeExtension`]: extracts an extension of the given type if it exists or [`None`] otherwise.
//! * [`HttpExtension`]: extracts an http extension of the given type coming from the request
//! (Similar to axum's [`extract::Extension`](https://docs.rs/axum/latest/axum/struct.Extension.html).
//! * [`MaybeHttpExtension`]: extracts an http extension of the given type if it exists or [`None`] otherwise.
//!
//! ### You can also implement your own Extractor!
//! Implement the [`FromConnectParts`], [`FromMessageParts`], [`FromMessage`] and [`FromDisconnectParts`] traits
//! on any type to extract data from the context of the handler.
//!
//! When implementing these traits, if you clone the [`Arc<Socket>`](crate::socket::Socket) make sure
//! that it is dropped at least when the socket is disconnected.
//! Otherwise it will create a memory leak. It is why the [`SocketRef`] extractor is used instead of cloning
//! the socket for common usage.
//!
//! If you want to deserialize the [`Value`](socketioxide_core::Value) data you must manually call
//! the `Data` extractor to deserialize it.
//!
//! [`FromConnectParts`]: crate::handler::FromConnectParts
//! [`FromMessageParts`]: crate::handler::FromMessageParts
//! [`FromMessage`]: crate::handler::FromMessage
//! [`FromDisconnectParts`]: crate::handler::FromDisconnectParts
//! [`ConnectHandler`]: crate::handler::ConnectHandler
//! [`ConnectMiddleware`]: crate::handler::ConnectMiddleware
//! [`MessageHandler`]: crate::handler::MessageHandler
//! [`DisconnectHandler`]: crate::handler::DisconnectHandler
//!
//! #### Example that extracts a user id from the query params
//! ```rust
//! # use bytes::Bytes;
//! # use socketioxide::handler::{FromConnectParts, FromMessageParts, Value};
//! # use socketioxide::adapter::Adapter;
//! # use socketioxide::socket::Socket;
//! # use std::sync::Arc;
//! # use std::convert::Infallible;
//! # use socketioxide::SocketIo;
//! struct UserId(String);
//!
//! #[derive(Debug)]
//! struct UserIdNotFound;
//! impl std::fmt::Display for UserIdNotFound {
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! write!(f, "User id not found")
//! }
//! }
//! impl std::error::Error for UserIdNotFound {}
//!
//! impl<A: Adapter> FromConnectParts<A> for UserId {
//! type Error = Infallible;
//! fn from_connect_parts(s: &Arc<Socket<A>>, _: &Option<Value>) -> Result<Self, Self::Error> {
//! // In a real app it would be better to parse the query params with a crate like `url`
//! let uri = &s.req_parts().uri;
//! let uid = uri
//! .query()
//! .and_then(|s| s.split('&').find(|s| s.starts_with("id=")).map(|s| &s[3..]))
//! .unwrap_or_default();
//! // Currently, it is not possible to have lifetime on the extracted data
//! Ok(UserId(uid.to_string()))
//! }
//! }
//!
//! // Here, if the user id is not found, the handler won't be called
//! // and a tracing `error` log will be emitted (if the `tracing` feature is enabled)
//! impl<A: Adapter> FromMessageParts<A> for UserId {
//! type Error = UserIdNotFound;
//!
//! fn from_message_parts(s: &Arc<Socket<A>>, _: &mut Value, _: &Option<i64>) -> Result<Self, UserIdNotFound> {
//! // In a real app it would be better to parse the query params with a crate like `url`
//! let uri = &s.req_parts().uri;
//! let uid = uri
//! .query()
//! .and_then(|s| s.split('&').find(|s| s.starts_with("id=")).map(|s| &s[3..]))
//! .ok_or(UserIdNotFound)?;
//! // Currently, it is not possible to have lifetime on the extracted data
//! Ok(UserId(uid.to_string()))
//! }
//! }
//!
//! async fn handler(user_id: UserId) {
//! println!("User id: {}", user_id.0);
//! }
//! let (svc, io) = SocketIo::new_svc();
//! io.ns("/", handler);
//! // Use the service with your favorite http server
pub use *;
pub use *;
pub use *;
pub use *;
/// Private API.
pub use __impl_deref;