File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 133
133
.yarn /unplugged
134
134
.yarn /build-state.yml
135
135
.yarn /install-state.gz
136
- .pnp. *
136
+ .pnp. *
137
+
138
+ # RustRover
139
+ .idea
Original file line number Diff line number Diff line change @@ -63,7 +63,18 @@ impl WebSocket {
63
63
///
64
64
/// Response::error("never got a message echoed back :(", 500)
65
65
/// ```
66
- pub async fn connect ( mut url : Url ) -> Result < WebSocket > {
66
+ pub async fn connect ( url : Url ) -> Result < WebSocket > {
67
+ return WebSocket :: connect_with_protocols ( url, None ) ;
68
+ }
69
+
70
+ /// Attempts to establish a [`WebSocket`] connection to the provided [`Url`] and protocol.
71
+ ///
72
+ /// # Example:
73
+ /// ```rust,ignore
74
+ /// let ws = WebSocket::connect_with_protocols("wss://echo.zeb.workers.dev/".parse()?, Some(vec!["GiggleBytes"])).await?;
75
+ ///
76
+ /// ```
77
+ pub async fn connect_with_protocols ( mut url : Url , protocols : Option < Vec < & str > > ) -> Result < WebSocket > {
67
78
let scheme: String = match url. scheme ( ) {
68
79
"ws" => "http" . into ( ) ,
69
80
"wss" => "https" . into ( ) ,
@@ -77,6 +88,13 @@ impl WebSocket {
77
88
let mut req = Request :: new ( url. as_str ( ) , Method :: Get ) ?;
78
89
req. headers_mut ( ) ?. set ( "upgrade" , "websocket" ) ?;
79
90
91
+ match protocols {
92
+ None => { }
93
+ Some ( v) => {
94
+ req. headers_mut ( ) ?. set ( "Sec-WebSocket-Protocol" , protocols. join ( "," ) ) ?;
95
+ }
96
+ }
97
+
80
98
let res = Fetch :: Request ( req) . send ( ) . await ?;
81
99
82
100
match res. websocket ( ) {
You can’t perform that action at this time.
0 commit comments