diff --git a/src/WebSocketClient.cpp b/src/WebSocketClient.cpp index ab41b0a..d96937a 100644 --- a/src/WebSocketClient.cpp +++ b/src/WebSocketClient.cpp @@ -51,6 +51,10 @@ int WebSocketClient::begin(const char* aPath) sendHeader("Connection", "Upgrade"); sendHeader("Sec-WebSocket-Key", base64RandomKey); sendHeader("Sec-WebSocket-Version", "13"); + + if(hasAuth) { + sendBasicAuth(_username, _password); + } endRequest(); status = responseStatusCode(); @@ -72,6 +76,23 @@ int WebSocketClient::begin(const String& aPath) return begin(aPath.c_str()); } +int WebSocketClient::begin(const char* username, const char* password) +{ + hasAuth = true; + _username = username; + _password = password; + return begin("/"); +} + +int WebSocketClient::begin(const String& aPath, const char* username, const char* password) +{ + hasAuth = true; + _username = username; + _password = password; + return begin(aPath.c_str()); +} + + int WebSocketClient::beginMessage(int aType) { if (iTxStarted) diff --git a/src/WebSocketClient.h b/src/WebSocketClient.h index 4b009e6..704c4d4 100644 --- a/src/WebSocketClient.h +++ b/src/WebSocketClient.h @@ -28,6 +28,8 @@ class WebSocketClient : public HttpClient */ int begin(const char* aPath = "/"); int begin(const String& aPath); + int begin(const char* username, const char* password); + int begin(const String& aPath, const char* username, const char* password); /** Begin to send a message of type (TYPE_TEXT or TYPE_BINARY) Use the write or Stream API's to set message content, followed by endMessage @@ -84,6 +86,9 @@ class WebSocketClient : public HttpClient void flushRx(); private: + bool hasAuth = false; + const char* _username; + const char* _password; bool iTxStarted; uint8_t iTxMessageType; uint8_t iTxBuffer[128];