Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,11 @@ void WebSocketsClient::messageReceived(WSclient_t * client, WSopcode_t opcode, u
}

/**
* Disconnect an client
* Disconnect a client
* @param client WSclient_t * ptr to the client struct
* @param reason const char * disconnect reason (optional, defaults to NULL)
*/
void WebSocketsClient::clientDisconnect(WSclient_t * client) {
void WebSocketsClient::clientDisconnect(WSclient_t * client, const char * reason) {
bool event = false;

#ifdef HAS_SSL
Expand Down Expand Up @@ -584,7 +585,11 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) {

DEBUG_WEBSOCKETS("[WS-Client] client disconnected.\n");
if(event) {
runCbEvent(WStype_DISCONNECTED, NULL, 0);
if(reason && strlen(reason) > 0) {
runCbEvent(WStype_DISCONNECTED, (uint8_t *)reason, strlen(reason));
} else {
runCbEvent(WStype_DISCONNECTED, NULL, 0);
}
}
}

Expand All @@ -607,13 +612,13 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
if(client->status != WSC_NOT_CONNECTED) {
DEBUG_WEBSOCKETS("[WS-Client] connection lost.\n");
// do cleanup
clientDisconnect(client);
clientDisconnect(client, "Connection lost");
}
}

if(client->tcp) {
// do cleanup
clientDisconnect(client);
clientDisconnect(client, "TCP connection cleanup");
}

return false;
Expand All @@ -625,7 +630,7 @@ bool WebSocketsClient::clientIsConnected(WSclient_t * client) {
void WebSocketsClient::handleClientData(void) {
if((_client.status == WSC_HEADER || _client.status == WSC_BODY) && _lastHeaderSent + WEBSOCKETS_TCP_TIMEOUT < millis()) {
DEBUG_WEBSOCKETS("[WS-Client][handleClientData] header response timeout.. disconnecting!\n");
clientDisconnect(&_client);
clientDisconnect(&_client, "Header response timeout");
WEBSOCKETS_YIELD();
return;
}
Expand Down Expand Up @@ -860,7 +865,9 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
default: ///< Server dont unterstand requrst
ok = false;
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] serverCode is not 101 (%d)\n", client->cCode);
clientDisconnect(client);
// Create disconnect reason with HTTP status code
String reason = "HTTP " + String(client->cCode);
clientDisconnect(client, reason.c_str());
_lastConnectionFail = millis();
break;
}
Expand Down Expand Up @@ -904,7 +911,11 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
if(clientIsConnected(client)) {
write(client, "This is a webSocket client!");
}
clientDisconnect(client);
String reason = "WebSocket handshake failed";
if(client->cCode > 0) {
reason += " - HTTP " + String(client->cCode);
}
clientDisconnect(client, reason.c_str());
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/WebSocketsClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ class WebSocketsClient : protected WebSockets {

void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);

void clientDisconnect(WSclient_t * client);
void clientDisconnect(WSclient_t * client) {
clientDisconnect(client, NULL);
}
void clientDisconnect(WSclient_t * client, const char * reason = NULL);
bool clientIsConnected(WSclient_t * client);

#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
Expand Down