@@ -91,6 +91,14 @@ void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String f
9191}
9292#endif
9393
94+ void WebSocketsClient::beginSocketIO (const char *host, uint16_t port, const char * url, const char * protocol) {
95+ begin (host, port, url, protocol);
96+ _client.isSocketIO = true ;
97+ }
98+
99+ void WebSocketsClient::beginSocketIO (String host, uint16_t port, String url, String protocol) {
100+ beginSocketIO (host.c_str (), port, url.c_str (), protocol.c_str ());
101+ }
94102
95103#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
96104/* *
@@ -393,23 +401,38 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
393401 unsigned long start = micros ();
394402#endif
395403
396- String handshake = " GET " + client->cUrl + " HTTP/1.1\r\n "
397- " Host: " + _host + " :" + _port + " \r\n "
398- " Connection: Upgrade\r\n "
399- " Upgrade: websocket\r\n "
400- " Origin: file://\r\n "
401- " User-Agent: arduino-WebSocket-Client\r\n "
402- " Sec-WebSocket-Version: 13\r\n "
403- " Sec-WebSocket-Key: " + client->cKey + " \r\n " ;
404-
405- if (client->cProtocol .length () > 0 ) {
406- handshake += " Sec-WebSocket-Protocol: " + client->cProtocol + " \r\n " ;
407- }
404+ String transport;
405+ String handshake;
406+ if (!client->isSocketIO || (client->isSocketIO && client->cSessionId .length () > 0 )) {
407+ if (client->isSocketIO ) {
408+ transport = " &transport=websocket&sid=" + client->cSessionId ;
409+ }
410+ handshake = " GET " + client->cUrl + transport + " HTTP/1.1\r\n "
411+ " Host: " + _host + " :" + _port + " \r\n "
412+ " Connection: Upgrade\r\n "
413+ " Upgrade: websocket\r\n "
414+ " Origin: file://\r\n "
415+ " User-Agent: arduino-WebSocket-Client\r\n "
416+ " Sec-WebSocket-Version: 13\r\n "
417+ " Sec-WebSocket-Key: " + client->cKey + " \r\n " ;
418+
419+ if (client->cProtocol .length () > 0 ) {
420+ handshake += " Sec-WebSocket-Protocol: " + client->cProtocol + " \r\n " ;
421+ }
422+
423+ if (client->cExtensions .length () > 0 ) {
424+ handshake += " Sec-WebSocket-Extensions: " + client->cExtensions + " \r\n " ;
425+ }
408426
409- if (client->cExtensions .length () > 0 ) {
410- handshake += " Sec-WebSocket-Extensions: " + client->cExtensions + " \r\n " ;
427+ } else {
428+ handshake = " GET " + client->cUrl + " &transport=polling HTTP/1.1\r\n "
429+ " Connection: keep-alive\r\n " ;
411430 }
412431
432+ handshake += " Host: " + _host + " :" + _port + " \r\n "
433+ " Origin: file://\r\n "
434+ " User-Agent: arduino-WebSocket-Client\r\n " ;
435+
413436 if (client->base64Authorization .length () > 0 ) {
414437 handshake += " Authorization: Basic " + client->base64Authorization + " \r\n " ;
415438 }
@@ -465,6 +488,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
465488 client->cExtensions = headerValue;
466489 } else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Version" )) {
467490 client->cVersion = headerValue.toInt ();
491+ } else if (headerName.equalsIgnoreCase (" Set-Cookie" )) {
492+ client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
468493 }
469494 } else {
470495 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine->c_str ());
@@ -490,6 +515,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
490515 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cProtocol: %s\n " , client->cProtocol .c_str ());
491516 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cExtensions: %s\n " , client->cExtensions .c_str ());
492517 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cVersion: %d\n " , client->cVersion );
518+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cSessionId: %s\n " , client->cSessionId .c_str ());
493519
494520 bool ok = (client->cIsUpgrade && client->cIsWebsocket );
495521
@@ -498,6 +524,10 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
498524 case 101 : // /< Switching Protocols
499525
500526 break ;
527+ case 200 :
528+ if (client->isSocketIO ) {
529+ break ;
530+ }
501531 case 403 : // /< Forbidden
502532 // todo handle login
503533 default : // /< Server dont unterstand requrst
@@ -530,6 +560,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
530560
531561 runCbEvent (WStype_CONNECTED, (uint8_t *) client->cUrl .c_str (), client->cUrl .length ());
532562
563+ } else if (clientIsConnected (client) && client->isSocketIO && client->cSessionId .length () > 0 ) {
564+ sendHeader (client);
533565 } else {
534566 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] no Websocket connection close.\n " );
535567 client->tcp ->write (" This is a webSocket client!" );
0 commit comments