@@ -265,6 +265,15 @@ impl<F> WebSocketUpgrade<F> {
265
265
self
266
266
}
267
267
268
+ /// Return the selected WebSocket subprotocol, if one has been chosen.
269
+ ///
270
+ /// If [`protocols()`][Self::protocols] has been called and a matching
271
+ /// protocol has been selected, the return value will be `Some` containing
272
+ /// said protocol. Otherwise, it will be `None`.
273
+ pub fn selected_protocol ( & self ) -> Option < & HeaderValue > {
274
+ self . protocol . as_ref ( )
275
+ }
276
+
268
277
/// Provide a callback to call if upgrading the connection fails.
269
278
///
270
279
/// The connection upgrade is performed in a background task. If that fails this callback
@@ -1154,6 +1163,7 @@ mod tests {
1154
1163
1155
1164
fn echo_app ( ) -> Router {
1156
1165
async fn handle_socket ( mut socket : WebSocket ) {
1166
+ assert_eq ! ( socket. protocol( ) . unwrap( ) , "echo" ) ;
1157
1167
while let Some ( Ok ( msg) ) = socket. recv ( ) . await {
1158
1168
match msg {
1159
1169
Message :: Text ( _) | Message :: Binary ( _) | Message :: Close ( _) => {
@@ -1171,7 +1181,9 @@ mod tests {
1171
1181
Router :: new ( ) . route (
1172
1182
"/echo" ,
1173
1183
any ( |ws : WebSocketUpgrade | {
1174
- ready ( ws. protocols ( [ "echo2" , "echo" ] ) . on_upgrade ( handle_socket) )
1184
+ let ws = ws. protocols ( [ "echo2" , "echo" ] ) ;
1185
+ assert_eq ! ( ws. selected_protocol( ) . unwrap( ) , "echo" ) ;
1186
+ ready ( ws. on_upgrade ( handle_socket) )
1175
1187
} ) ,
1176
1188
)
1177
1189
}
0 commit comments