@@ -532,10 +532,11 @@ void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, u
532532}
533533
534534/* *
535- * Disconnect an client
535+ * Disconnect a client
536536 * @param client WSclient_t * ptr to the client struct
537+ * @param reason const char * disconnect reason (optional, defaults to NULL)
537538 */
538- void WebSocketsClient::clientDisconnect (WSclient_t * client) {
539+ void WebSocketsClient::clientDisconnect (WSclient_t * client, const char * reason ) {
539540 bool event = false ;
540541
541542#ifdef HAS_SSL
@@ -584,7 +585,11 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {
584585
585586 DEBUG_WEBSOCKETS (" [WS-Client] client disconnected.\n " );
586587 if (event) {
587- runCbEvent (WStype_DISCONNECTED, NULL , 0 );
588+ if (reason && strlen (reason) > 0 ) {
589+ runCbEvent (WStype_DISCONNECTED, (uint8_t *)reason, strlen (reason));
590+ } else {
591+ runCbEvent (WStype_DISCONNECTED, NULL , 0 );
592+ }
588593 }
589594}
590595
@@ -607,13 +612,13 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
607612 if (client->status != WSC_NOT_CONNECTED) {
608613 DEBUG_WEBSOCKETS (" [WS-Client] connection lost.\n " );
609614 // do cleanup
610- clientDisconnect (client);
615+ clientDisconnect (client, " Connection lost " );
611616 }
612617 }
613618
614619 if (client->tcp ) {
615620 // do cleanup
616- clientDisconnect (client);
621+ clientDisconnect (client, " TCP connection cleanup " );
617622 }
618623
619624 return false ;
@@ -625,7 +630,7 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
625630void WebSocketsClient::handleClientData (void ) {
626631 if ((_client.status == WSC_HEADER || _client.status == WSC_BODY) && _lastHeaderSent + WEBSOCKETS_TCP_TIMEOUT < millis ()) {
627632 DEBUG_WEBSOCKETS (" [WS-Client][handleClientData] header response timeout.. disconnecting!\n " );
628- clientDisconnect (&_client);
633+ clientDisconnect (&_client, " Header response timeout " );
629634 WEBSOCKETS_YIELD ();
630635 return ;
631636 }
@@ -860,7 +865,9 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
860865 default : // /< Server dont unterstand requrst
861866 ok = false ;
862867 DEBUG_WEBSOCKETS (" [WS-Client][handleHeader] serverCode is not 101 (%d)\n " , client->cCode );
863- clientDisconnect (client);
868+ // Create disconnect reason with HTTP status code
869+ String reason = " HTTP " + String (client->cCode );
870+ clientDisconnect (client, reason.c_str ());
864871 _lastConnectionFail = millis ();
865872 break ;
866873 }
@@ -904,7 +911,11 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
904911 if (clientIsConnected (client)) {
905912 write (client, " This is a webSocket client!" );
906913 }
907- clientDisconnect (client);
914+ String reason = " WebSocket handshake failed" ;
915+ if (client->cCode > 0 ) {
916+ reason += " - HTTP " + String (client->cCode );
917+ }
918+ clientDisconnect (client, reason.c_str ());
908919 }
909920 }
910921}
0 commit comments