File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -324,6 +324,15 @@ impl Message {
324
324
self . inner . is_pong ( )
325
325
}
326
326
327
+ /// Try to get the close frame (close code and reason)
328
+ pub fn close_frame ( & self ) -> Option < ( u16 , & str ) > {
329
+ if let protocol:: Message :: Close ( Some ( ref close_frame) ) = self . inner {
330
+ Some ( ( close_frame. code . into ( ) , close_frame. reason . as_ref ( ) ) )
331
+ } else {
332
+ None
333
+ }
334
+ }
335
+
327
336
/// Try to get a reference to the string text, if this is a Text message.
328
337
pub fn to_str ( & self ) -> Result < & str , ( ) > {
329
338
match self . inner {
Original file line number Diff line number Diff line change @@ -81,6 +81,21 @@ async fn binary() {
81
81
assert_eq ! ( msg. as_bytes( ) , & b"bonk" [ ..] ) ;
82
82
}
83
83
84
+ #[ tokio:: test]
85
+ async fn close_frame ( ) {
86
+ let _ = pretty_env_logger:: try_init ( ) ;
87
+
88
+ let route = warp:: ws ( ) . map ( |ws : warp:: ws:: Ws | {
89
+ ws. on_upgrade ( |mut websocket| async move {
90
+ let msg = websocket. next ( ) . await . expect ( "item" ) . expect ( "ok" ) ;
91
+ let _ = msg. close_frame ( ) . expect ( "close frame" ) ;
92
+ } )
93
+ } ) ;
94
+
95
+ let client = warp:: test:: ws ( ) . handshake ( route) . await . expect ( "handshake" ) ;
96
+ drop ( client) ;
97
+ }
98
+
84
99
#[ tokio:: test]
85
100
async fn send_ping ( ) {
86
101
let _ = pretty_env_logger:: try_init ( ) ;
You can’t perform that action at this time.
0 commit comments