@@ -91,6 +91,14 @@ void WebSocketsClient::beginSSL(String host, uint16_t port, String url, String f
91
91
}
92
92
#endif
93
93
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
+ }
94
102
95
103
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
96
104
/* *
@@ -393,23 +401,38 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
393
401
unsigned long start = micros ();
394
402
#endif
395
403
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
+ }
408
426
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 " ;
411
430
}
412
431
432
+ handshake += " Host: " + _host + " :" + _port + " \r\n "
433
+ " Origin: file://\r\n "
434
+ " User-Agent: arduino-WebSocket-Client\r\n " ;
435
+
413
436
if (client->base64Authorization .length () > 0 ) {
414
437
handshake += " Authorization: Basic " + client->base64Authorization + " \r\n " ;
415
438
}
@@ -465,6 +488,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
465
488
client->cExtensions = headerValue;
466
489
} else if (headerName.equalsIgnoreCase (" Sec-WebSocket-Version" )) {
467
490
client->cVersion = headerValue.toInt ();
491
+ } else if (headerName.equalsIgnoreCase (" Set-Cookie" )) {
492
+ client->cSessionId = headerValue.substring (headerValue.indexOf (' =' ) + 1 );
468
493
}
469
494
} else {
470
495
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine->c_str ());
@@ -490,6 +515,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
490
515
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cProtocol: %s\n " , client->cProtocol .c_str ());
491
516
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cExtensions: %s\n " , client->cExtensions .c_str ());
492
517
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cVersion: %d\n " , client->cVersion );
518
+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] - cSessionId: %s\n " , client->cSessionId .c_str ());
493
519
494
520
bool ok = (client->cIsUpgrade && client->cIsWebsocket );
495
521
@@ -498,6 +524,10 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
498
524
case 101 : // /< Switching Protocols
499
525
500
526
break ;
527
+ case 200 :
528
+ if (client->isSocketIO ) {
529
+ break ;
530
+ }
501
531
case 403 : // /< Forbidden
502
532
// todo handle login
503
533
default : // /< Server dont unterstand requrst
@@ -530,6 +560,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
530
560
531
561
runCbEvent (WStype_CONNECTED, (uint8_t *) client->cUrl .c_str (), client->cUrl .length ());
532
562
563
+ } else if (clientIsConnected (client) && client->isSocketIO && client->cSessionId .length () > 0 ) {
564
+ sendHeader (client);
533
565
} else {
534
566
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] no Websocket connection close.\n " );
535
567
client->tcp ->write (" This is a webSocket client!" );
0 commit comments