@@ -505,7 +505,8 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
505505 client->cIsWebsocket = false ;
506506 client->cSessionId = " " ;
507507
508- client->status = WSC_NOT_CONNECTED;
508+ client->status = WSC_NOT_CONNECTED;
509+ _lastConnectionFail = millis ();
509510
510511 DEBUG_WEBSOCKETS (" [WS-Client] client disconnected.\n " );
511512 if (event) {
@@ -548,19 +549,26 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
548549 * Handel incomming data from Client
549550 */
550551void WebSocketsClient::handleClientData (void ) {
551- if (_client.status == WSC_HEADER && _lastHeaderSent + WEBSOCKETS_TCP_TIMEOUT < millis ()) {
552+ if (( _client.status == WSC_HEADER || _client. status == WSC_BODY) && _lastHeaderSent + WEBSOCKETS_TCP_TIMEOUT < millis ()) {
552553 DEBUG_WEBSOCKETS (" [WS-Client][handleClientData] header response timeout.. disconnecting!\n " );
553554 clientDisconnect (&_client);
554555 WEBSOCKETS_YIELD ();
555556 return ;
556557 }
558+
557559 int len = _client.tcp ->available ();
558560 if (len > 0 ) {
559561 switch (_client.status ) {
560562 case WSC_HEADER: {
561563 String headerLine = _client.tcp ->readStringUntil (' \n ' );
562564 handleHeader (&_client, &headerLine);
563565 } break ;
566+ case WSC_BODY: {
567+ char buf[256 ] = { 0 };
568+ _client.tcp ->readBytes (&buf[0 ], std::min ((size_t )len, sizeof (buf)));
569+ String bodyLine = buf;
570+ handleHeader (&_client, &bodyLine);
571+ } break ;
564572 case WSC_CONNECTED:
565573 WebSockets::handleWebsocket (&_client);
566574 break ;
@@ -672,6 +680,22 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
672680void WebSocketsClient::handleHeader (WSclient_t * client, String * headerLine) {
673681 headerLine->trim (); // remove \r
674682
683+ // this code handels the http body for Socket.IO V3 requests
684+ if (headerLine->length () > 0 && client->isSocketIO && client->status == WSC_BODY && client->cSessionId .length () == 0 ) {
685+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] socket.io json: %s\n " , headerLine->c_str ());
686+ String sid_begin = WEBSOCKETS_STRING (" \" sid\" :\" " );
687+ if (headerLine->indexOf (sid_begin) > -1 ) {
688+ int start = headerLine->indexOf (sid_begin) + sid_begin.length ();
689+ int end = headerLine->indexOf (' "' , start);
690+ client->cSessionId = headerLine->substring (start, end);
691+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cSessionId: %s\n " , client->cSessionId .c_str ());
692+
693+ // Trigger websocket connection code path
694+ *headerLine = " " ;
695+ }
696+ }
697+
698+ // headle HTTP header
675699 if (headerLine->length () > 0 ) {
676700 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine->c_str ());
677701
@@ -736,6 +760,14 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
736760 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cVersion: %d\n " , client->cVersion );
737761 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cSessionId: %s\n " , client->cSessionId .c_str ());
738762
763+ if (client->isSocketIO && client->cSessionId .length () == 0 && clientIsConnected (client)) {
764+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] still missing cSessionId try socket.io V3\n " );
765+ client->status = WSC_BODY;
766+ return ;
767+ } else {
768+ client->status = WSC_HEADER;
769+ }
770+
739771 bool ok = (client->cIsUpgrade && client->cIsWebsocket );
740772
741773 if (ok) {
@@ -777,15 +809,18 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
777809
778810 runCbEvent (WStype_CONNECTED, (uint8_t *)client->cUrl .c_str (), client->cUrl .length ());
779811#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
780- } else if (clientIsConnected (client) && client->isSocketIO && client->cSessionId .length () > 0 ) {
781- if (_client.tcp ->available ()) {
782- // read not needed data
783- DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] still data in buffer (%d), clean up.\n " , _client.tcp ->available ());
784- while (_client.tcp ->available () > 0 ) {
785- _client.tcp ->read ();
812+ } else if (client->isSocketIO ) {
813+ if (client->cSessionId .length () > 0 ) {
814+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] found cSessionId\n " );
815+ if (clientIsConnected (client) && _client.tcp ->available ()) {
816+ // read not needed data
817+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] still data in buffer (%d), clean up.\n " , _client.tcp ->available ());
818+ while (_client.tcp ->available () > 0 ) {
819+ _client.tcp ->read ();
820+ }
786821 }
822+ sendHeader (client);
787823 }
788- sendHeader (client);
789824#endif
790825 } else {
791826 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] no Websocket connection close.\n " );
0 commit comments