@@ -44,9 +44,23 @@ use crate::{
44
44
use futures:: FutureExt ;
45
45
use wasm_bindgen:: JsCast ;
46
46
47
- async fn connect ( addr : Multiaddr , timeout : Duration ) -> Result < ( Multiaddr , BrowserStream ) > {
47
+ async fn connect (
48
+ addr : Multiaddr ,
49
+ timeout : Duration ,
50
+ ty : TransportType ,
51
+ ) -> Result < ( Multiaddr , BrowserStream ) > {
52
+ let schema = match ty {
53
+ TransportType :: Ws => "ws" ,
54
+ TransportType :: Wss => "wss" ,
55
+ _ => unreachable ! ( ) ,
56
+ } ;
48
57
let url = match multiaddr_to_socketaddr ( & addr) {
49
- Some ( socket_address) => format ! ( "ws://{}:{}" , socket_address. ip( ) , socket_address. port( ) ) ,
58
+ Some ( socket_address) => format ! (
59
+ "{}://{}:{}" ,
60
+ schema,
61
+ socket_address. ip( ) ,
62
+ socket_address. port( )
63
+ ) ,
50
64
None => {
51
65
let mut iter = addr. iter ( ) . peekable ( ) ;
52
66
@@ -72,10 +86,10 @@ async fn connect(addr: Multiaddr, timeout: Duration) -> Result<(Multiaddr, Brows
72
86
73
87
match ( proto1, proto2) {
74
88
( Protocol :: Dns4 ( domain) , Protocol :: Tcp ( port) ) => {
75
- break format ! ( "ws ://{}:{}" , domain, port)
89
+ break format ! ( "{} ://{}:{}" , schema , domain, port)
76
90
}
77
91
( Protocol :: Dns6 ( domain) , Protocol :: Tcp ( port) ) => {
78
- break format ! ( "ws ://{}:{}" , domain, port)
92
+ break format ! ( "{} ://{}:{}" , schema , domain, port)
79
93
}
80
94
_ => return Err ( TransportErrorKind :: NotSupported ( addr. clone ( ) ) ) ,
81
95
}
@@ -127,14 +141,24 @@ impl TransportDial for BrowserTransport {
127
141
type DialFuture = BrowserDialFuture ;
128
142
129
143
fn dial ( self , address : Multiaddr ) -> Result < Self :: DialFuture > {
130
- if !matches ! ( find_type( & address) , TransportType :: Ws ) {
131
- return Err ( TransportErrorKind :: NotSupported ( address) ) ;
132
- }
133
- let dial = crate :: runtime:: spawn ( connect ( address, self . timeout ) ) ;
144
+ match find_type ( & address) {
145
+ TransportType :: Ws => {
146
+ let dial = crate :: runtime:: spawn ( connect ( address, self . timeout , TransportType :: Ws ) ) ;
134
147
135
- Ok ( TransportFuture :: new ( Box :: pin ( async {
136
- dial. await . expect ( "oneshot channel panic" )
137
- } ) ) )
148
+ Ok ( TransportFuture :: new ( Box :: pin ( async {
149
+ dial. await . expect ( "oneshot channel panic" )
150
+ } ) ) )
151
+ }
152
+ TransportType :: Wss => {
153
+ let dial =
154
+ crate :: runtime:: spawn ( connect ( address, self . timeout , TransportType :: Wss ) ) ;
155
+
156
+ Ok ( TransportFuture :: new ( Box :: pin ( async {
157
+ dial. await . expect ( "oneshot channel panic" )
158
+ } ) ) )
159
+ }
160
+ _ => Err ( TransportErrorKind :: NotSupported ( address) ) ,
161
+ }
138
162
}
139
163
}
140
164
0 commit comments