@@ -586,6 +586,23 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
586
586
return ESP_OK ;
587
587
}
588
588
589
+ static void esp_websocket_client_prepare_transport (esp_websocket_client_handle_t client )
590
+ {
591
+ //get transport by scheme
592
+ if (client -> transport == NULL && client -> config -> ext_transport == NULL ) {
593
+ client -> transport = esp_transport_list_get_transport (client -> transport_list , client -> config -> scheme );
594
+ }
595
+
596
+ if (client -> transport == NULL ) {
597
+ ESP_LOGE (TAG , "There are no transports valid, stop websocket client" );
598
+ client -> run = false;
599
+ }
600
+ //default port
601
+ if (client -> config -> port == 0 ) {
602
+ client -> config -> port = esp_transport_get_default_port (client -> transport );
603
+ }
604
+ }
605
+
589
606
static int esp_websocket_client_send_with_exact_opcode (esp_websocket_client_handle_t client , ws_transport_opcodes_t opcode , const uint8_t * data , int len , TickType_t timeout )
590
607
{
591
608
int ret = -1 ;
@@ -985,19 +1002,7 @@ static void esp_websocket_client_task(void *pv)
985
1002
esp_websocket_client_handle_t client = (esp_websocket_client_handle_t ) pv ;
986
1003
client -> run = true;
987
1004
988
- //get transport by scheme
989
- if (client -> transport == NULL && client -> config -> ext_transport == NULL ) {
990
- client -> transport = esp_transport_list_get_transport (client -> transport_list , client -> config -> scheme );
991
- }
992
-
993
- if (client -> transport == NULL ) {
994
- ESP_LOGE (TAG , "There are no transports valid, stop websocket client" );
995
- client -> run = false;
996
- }
997
- //default port
998
- if (client -> config -> port == 0 ) {
999
- client -> config -> port = esp_transport_get_default_port (client -> transport );
1000
- }
1005
+ esp_websocket_client_prepare_transport (client );
1001
1006
1002
1007
client -> state = WEBSOCKET_STATE_INIT ;
1003
1008
xEventGroupClearBits (client -> status_bits , STOPPED_BIT | CLOSE_FRAME_SENT_BIT );
@@ -1024,10 +1029,26 @@ static void esp_websocket_client_task(void *pv)
1024
1029
esp_tls_error_handle_t error_handle = esp_transport_get_error_handle (client -> transport );
1025
1030
client -> error_handle .esp_ws_handshake_status_code = esp_transport_ws_get_upgrade_request_status (client -> transport );
1026
1031
if (error_handle ) {
1027
- esp_websocket_client_error (client , "esp_transport_connect() failed with %d, "
1028
- "transport_error=%s, tls_error_code=%i, tls_flags=%i, esp_ws_handshake_status_code=%d, errno=%d" ,
1029
- result , esp_err_to_name (error_handle -> last_error ), error_handle -> esp_tls_error_code ,
1030
- error_handle -> esp_tls_flags , client -> error_handle .esp_ws_handshake_status_code , errno );
1032
+ if (client -> error_handle .esp_ws_handshake_status_code == WS_TRANSPORT_RESPONSE_REDIR ) {
1033
+ client -> config -> port = 0 ;
1034
+ client -> config -> uri = esp_transport_ws_get_redir_uri (client -> transport );
1035
+ if (client -> config -> uri == NULL ) {
1036
+ ESP_LOGE (TAG , "There are no redirect uri" );
1037
+ } else {
1038
+ ESP_LOGI (TAG , "Redirecting to %s" , client -> config -> uri );
1039
+ esp_websocket_client_set_uri (client , client -> config -> uri );
1040
+ esp_websocket_client_prepare_transport (client );
1041
+
1042
+ // Rerun the connection with the redir uri.
1043
+ client -> state = WEBSOCKET_STATE_INIT ;
1044
+ break ;
1045
+ }
1046
+ } else {
1047
+ esp_websocket_client_error (client , "esp_transport_connect() failed with %d, "
1048
+ "transport_error=%s, tls_error_code=%i, tls_flags=%i, esp_ws_handshake_status_code=%d, errno=%d" ,
1049
+ result , esp_err_to_name (error_handle -> last_error ), error_handle -> esp_tls_error_code ,
1050
+ error_handle -> esp_tls_flags , client -> error_handle .esp_ws_handshake_status_code , errno );
1051
+ }
1031
1052
} else {
1032
1053
esp_websocket_client_error (client , "esp_transport_connect() failed with %d, esp_ws_handshake_status_code=%d, errno=%d" ,
1033
1054
result , client -> error_handle .esp_ws_handshake_status_code , errno );
0 commit comments