@@ -44,9 +44,23 @@ use crate::{
4444use futures:: FutureExt ;
4545use wasm_bindgen:: JsCast ;
4646
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+ } ;
4857 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+ ) ,
5064 None => {
5165 let mut iter = addr. iter ( ) . peekable ( ) ;
5266
@@ -72,10 +86,10 @@ async fn connect(addr: Multiaddr, timeout: Duration) -> Result<(Multiaddr, Brows
7286
7387 match ( proto1, proto2) {
7488 ( Protocol :: Dns4 ( domain) , Protocol :: Tcp ( port) ) => {
75- break format ! ( "ws ://{}:{}" , domain, port)
89+ break format ! ( "{} ://{}:{}" , schema , domain, port)
7690 }
7791 ( Protocol :: Dns6 ( domain) , Protocol :: Tcp ( port) ) => {
78- break format ! ( "ws ://{}:{}" , domain, port)
92+ break format ! ( "{} ://{}:{}" , schema , domain, port)
7993 }
8094 _ => return Err ( TransportErrorKind :: NotSupported ( addr. clone ( ) ) ) ,
8195 }
@@ -127,14 +141,24 @@ impl TransportDial for BrowserTransport {
127141 type DialFuture = BrowserDialFuture ;
128142
129143 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 ) ) ;
134147
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+ }
138162 }
139163}
140164
0 commit comments