@@ -19,6 +19,7 @@ public TransportRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSo
1919 _clientWebSocketProvider = clientWebSocketProvider ;
2020 UriConverter = new UriConverter ( ) ;
2121 Path = "/socket.io" ;
22+ ConnectionTimeout = TimeSpan . FromSeconds ( 20 ) ;
2223 _messageQueue = new Queue < IMessage > ( ) ;
2324 }
2425
@@ -43,6 +44,8 @@ public TransportRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSo
4344
4445 public IEnumerable < KeyValuePair < string , string > > QueryParams { get ; set ; }
4546
47+ public TimeSpan ConnectionTimeout { get ; set ; }
48+
4649 public TransportProtocol Protocol { get ; private set ; }
4750
4851 public string Sid { get ; private set ; }
@@ -62,15 +65,27 @@ public async Task ConnectAsync()
6265 _webSocketTransport . Dispose ( ) ;
6366 }
6467 Uri uri = UriConverter . GetHandshakeUri ( ServerUri , Path , QueryParams ) ;
65- string text = await _httpClient . GetStringAsync ( uri ) . ConfigureAwait ( false ) ;
68+
69+ var req = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
70+
71+ var resMsg = await _httpClient . SendAsync ( req , new CancellationTokenSource ( ConnectionTimeout ) . Token ) . ConfigureAwait ( false ) ;
72+ if ( ! resMsg . IsSuccessStatusCode )
73+ {
74+ throw new HttpRequestException ( $ "Response status code does not indicate success: { resMsg . StatusCode } ") ;
75+ }
76+ string text = await resMsg . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
77+
6678 int index = text . IndexOf ( '{' ) ;
6779 string json = text . Substring ( index ) ;
6880 var info = System . Text . Json . JsonSerializer . Deserialize < HandshakeInfo > ( json ) ;
6981 Sid = info . Sid ;
7082 if ( info . Upgrades . Contains ( "websocket" ) && AutoUpgrade )
7183 {
7284 _clientWebSocket = _clientWebSocketProvider ( ) ;
73- _webSocketTransport = new WebSocketTransport ( _clientWebSocket ) ;
85+ _webSocketTransport = new WebSocketTransport ( _clientWebSocket )
86+ {
87+ ConnectionTimeout = ConnectionTimeout
88+ } ;
7489 await WebSocketConnectAsync ( ) . ConfigureAwait ( false ) ;
7590 Protocol = TransportProtocol . WebSocket ;
7691 }
0 commit comments