@@ -505,7 +505,8 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
505
505
client->cIsWebsocket = false ;
506
506
client->cSessionId = " " ;
507
507
508
- client->status = WSC_NOT_CONNECTED;
508
+ client->status = WSC_NOT_CONNECTED;
509
+ _lastConnectionFail = millis ();
509
510
510
511
DEBUG_WEBSOCKETS (" [WS-Client] client disconnected.\n " );
511
512
if (event) {
@@ -548,19 +549,26 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
548
549
* Handel incomming data from Client
549
550
*/
550
551
void 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 ()) {
552
553
DEBUG_WEBSOCKETS (" [WS-Client][handleClientData] header response timeout.. disconnecting!\n " );
553
554
clientDisconnect (&_client);
554
555
WEBSOCKETS_YIELD ();
555
556
return ;
556
557
}
558
+
557
559
int len = _client.tcp ->available ();
558
560
if (len > 0 ) {
559
561
switch (_client.status ) {
560
562
case WSC_HEADER: {
561
563
String headerLine = _client.tcp ->readStringUntil (' \n ' );
562
564
handleHeader (&_client, &headerLine);
563
565
} 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 ;
564
572
case WSC_CONNECTED:
565
573
WebSockets::handleWebsocket (&_client);
566
574
break ;
@@ -672,6 +680,22 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
672
680
void WebSocketsClient::handleHeader (WSclient_t * client, String * headerLine) {
673
681
headerLine->trim (); // remove \r
674
682
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
675
699
if (headerLine->length () > 0 ) {
676
700
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine->c_str ());
677
701
@@ -736,6 +760,14 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
736
760
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cVersion: %d\n " , client->cVersion );
737
761
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cSessionId: %s\n " , client->cSessionId .c_str ());
738
762
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
+
739
771
bool ok = (client->cIsUpgrade && client->cIsWebsocket );
740
772
741
773
if (ok) {
@@ -777,15 +809,18 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
777
809
778
810
runCbEvent (WStype_CONNECTED, (uint8_t *)client->cUrl .c_str (), client->cUrl .length ());
779
811
#if (WEBSOCK
B800
ETS_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
+ }
786
821
}
822
+ sendHeader (client);
787
823
}
788
- sendHeader (client);
789
824
#endif
790
825
} else {
791
826
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] no Websocket connection close.\n " );
0 commit comments