@@ -40,7 +40,9 @@ WebSocketsClient::~WebSocketsClient() {
40
40
void WebSocketsClient::begin (const char *host, uint16_t port, const char * url) {
41
41
_host = host;
42
42
_port = port;
43
+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
43
44
_fingerprint = " " ;
45
+ #endif
44
46
45
47
_client.num = 0 ;
46
48
_client.status = WSC_NOT_CONNECTED;
@@ -141,10 +143,12 @@ void WebSocketsClient::loop(void) {
141
143
142
144
} else {
143
145
DEBUG_WEBSOCKETS (" [WS-Client] connection to %s:%u Faild\n " , _host.c_str (), _port);
144
- delay (10 ); // some litle delay to not flood the server
146
+ delay (10 ); // some little delay to not flood the server
145
147
}
146
148
} else {
149
+ #if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
147
150
handleClientData ();
151
+ #endif
148
152
}
149
153
}
150
154
@@ -248,12 +252,15 @@ void WebSocketsClient::messageRecived(WSclient_t * client, WSopcode_t opcode, ui
248
252
*/
249
253
void WebSocketsClient::clientDisconnect (WSclient_t * client) {
250
254
255
+ bool event = false ;
256
+
251
257
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
252
258
if (client->isSSL && client->ssl ) {
253
259
if (client->ssl ->connected ()) {
254
260
client->ssl ->flush ();
255
261
client->ssl ->stop ();
256
262
}
263
+ event = true ;
257
264
delete client->ssl ;
258
265
client->ssl = NULL ;
259
266
client->tcp = NULL ;
@@ -265,6 +272,7 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
265
272
client->tcp ->flush ();
266
273
client->tcp ->stop ();
267
274
}
275
+ event = true ;
268
276
delete client->tcp ;
269
277
client->tcp = NULL ;
270
278
}
@@ -280,9 +288,9 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
280
288
client->status = WSC_NOT_CONNECTED;
281
289
282
290
DEBUG_WEBSOCKETS (" [WS-Client] client disconnected.\n " );
283
-
284
- runCbEvent (WStype_DISCONNECTED, NULL , 0 );
285
-
291
+ if (event) {
292
+ runCbEvent (WStype_DISCONNECTED, NULL , 0 );
293
+ }
286
294
}
287
295
288
296
/* *
@@ -316,7 +324,7 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
316
324
317
325
return false ;
318
326
}
319
-
327
+ # if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
320
328
/* *
321
329
* Handel incomming data from Client
322
330
*/
@@ -325,7 +333,10 @@ void WebSocketsClient::handleClientData(void) {
325
333
if (len > 0 ) {
326
334
switch (_client.status ) {
327
335
case WSC_HEADER:
328
- handleHeader (&_client);
336
+ {
337
+ String headerLine = _client->tcp ->readStringUntil (' \n ' );
338
+ handleHeader (&_client, &headerLine);
339
+ }
329
340
break ;
330
341
case WSC_CONNECTED:
331
342
WebSockets::handleWebsocket (&_client);
@@ -335,10 +346,11 @@ void WebSocketsClient::handleClientData(void) {
335
346
break ;
336
347
}
337
348
}
338
- #ifdef ESP8266
349
+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
339
350
delay (0 );
340
351
#endif
341
352
}
353
+ #endif
342
354
343
355
/* *
344
356
* send the WebSocket header to Server
@@ -377,6 +389,10 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
377
389
378
390
client->tcp ->write (handshake.c_str (), handshake.length ());
379
391
392
+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
393
+ client->tcp ->readStringUntil (' \n ' , &(client->cHttpLine ), std::bind (&WebSocketsClient::handleHeader, this , client, &(client->cHttpLine )));
394
+ #endif
395
+
380
396
DEBUG_WEBSOCKETS (" [WS-Client][sendHeader] sending header... Done (%uus).\n " , (micros () - start));
381
397
382
398
}
@@ -385,20 +401,19 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
385
401
* handle the WebSocket header reading
386
402
* @param client WSclient_t * ptr to the client struct
387
403
*/
388
- void WebSocketsClient::handleHeader (WSclient_t * client) {
404
+ void WebSocketsClient::handleHeader (WSclient_t * client, String * headerLine ) {
389
405
390
- String headerLine = client->tcp ->readStringUntil (' \n ' );
391
- headerLine.trim (); // remove \r
406
+ headerLine->trim (); // remove \r
392
407
393
- if (headerLine. length () > 0 ) {
394
- DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine. c_str ());
408
+ if (headerLine-> length () > 0 ) {
409
+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] RX: %s\n " , headerLine-> c_str ());
395
410
396
- if (headerLine. startsWith (" HTTP/1." )) {
411
+ if (headerLine-> startsWith (" HTTP/1." )) {
397
412
// "HTTP/1.1 101 Switching Protocols"
398
- client->cCode = headerLine. substring (9 , headerLine. indexOf (' ' , 9 )).toInt ();
399
- } else if (headerLine. indexOf (' :' )) {
400
- String headerName = headerLine. substring (0 , headerLine. indexOf (' :' ));
401
- String headerValue = headerLine. substring (headerLine. indexOf (' :' ) + 2 );
413
+ client->cCode = headerLine-> substring (9 , headerLine-> indexOf (' ' , 9 )).toInt ();
414
+ } else if (headerLine-> indexOf (' :' )) {
415
+ String headerName = headerLine-> substring (0 , headerLine-> indexOf (' :' ));
416
+ String headerValue = headerLine-> substring (headerLine-> indexOf (' :' ) + 2 );
402
417
403
418
if (headerName.equalsIgnoreCase (" Connection" )) {
404
419
if (headerValue.indexOf (" Upgrade" ) >= 0 ) {
@@ -419,9 +434,14 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
419
434
client->cVersion = headerValue.toInt ();
420
435
}
421
436
} else {
422
- DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine. c_str ());
437
+ DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header error (%s)\n " , headerLine-> c_str ());
423
438
}
424
439
440
+ (*headerLine) = " " ;
441
+ #if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
442
+ client->tcp ->readStringUntil (' \n ' , &(client->cHttpLine ), std::bind (&WebSocketsClient::handleHeader, this , client, &(client->cHttpLine )));
443
+ #endif
444
+
425
445
} else {
426
446
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Header read fin.\n " );
427
447
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Client settings:\n " );
@@ -456,6 +476,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
456
476
}
457
477
458
478
if (ok) {
479
+
459
480
if (client->cAccept .length () == 0 ) {
460
481
ok = false ;
461
482
} else {
@@ -471,8 +492,8 @@ void WebSocketsClient::handleHeader(WSclient_t * client) {
471
492
if (ok) {
472
493
473
494
DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] Websocket connection init done.\n " );
495
+ headerDone (client);
474
496
475
- client->status = WSC_CONNECTED;
476
497
477
498
runCbEvent (WStype_CONNECTED, (uint8_t *) client->cUrl .c_str (), client->cUrl .length ());
478
499
0 commit comments