Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional headers in WebSocketClient #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 20 additions & 11 deletions src/WebSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint1
{
}

WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
: HttpClient(aClient, aServerName, aServerPort),
iTxStarted(false),
iRxSize(0)
Expand All @@ -26,7 +26,7 @@ WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddres
{
}

int WebSocketClient::begin(const char* aPath)
int WebSocketClient::begin(const char* aPath, char* (*additionalHeaders)[2], size_t headerRows)
{
// start the GET request
beginRequest();
Expand All @@ -51,6 +51,10 @@ int WebSocketClient::begin(const char* aPath)
sendHeader("Connection", "Upgrade");
sendHeader("Sec-WebSocket-Key", base64RandomKey);
sendHeader("Sec-WebSocket-Version", "13");
for (size_t i = 0; i < headerRows; ++i)
{
sendHeader(additionalHeaders[i][0], additionalHeaders[i][1]);
}
endRequest();

status = responseStatusCode();
Expand All @@ -67,9 +71,14 @@ int WebSocketClient::begin(const char* aPath)
return (status == 101) ? 0 : status;
}

int WebSocketClient::begin(const char* aPath)
{
return begin(aPath, NULL, 0);
}

int WebSocketClient::begin(const String& aPath)
{
return begin(aPath.c_str());
return begin(aPath.c_str(), NULL, 0);
}

int WebSocketClient::beginMessage(int aType)
Expand Down Expand Up @@ -174,7 +183,7 @@ size_t WebSocketClient::write(const uint8_t *aBuffer, size_t aSize)
memcpy(iTxBuffer + iTxSize, aBuffer, aSize);

iTxSize += aSize;

return aSize;
}

Expand Down Expand Up @@ -217,14 +226,14 @@ int WebSocketClient::parseMessage()
}
else
{
iRxSize = ((uint64_t)HttpClient::read() << 56) |
((uint64_t)HttpClient::read() << 48) |
((uint64_t)HttpClient::read() << 40) |
((uint64_t)HttpClient::read() << 32) |
((uint64_t)HttpClient::read() << 24) |
((uint64_t)HttpClient::read() << 16) |
iRxSize = ((uint64_t)HttpClient::read() << 56) |
((uint64_t)HttpClient::read() << 48) |
((uint64_t)HttpClient::read() << 40) |
((uint64_t)HttpClient::read() << 32) |
((uint64_t)HttpClient::read() << 24) |
((uint64_t)HttpClient::read() << 16) |
((uint64_t)HttpClient::read() << 8) |
(uint64_t)HttpClient::read();
(uint64_t)HttpClient::read();
}

// read in the mask, if present
Expand Down
7 changes: 7 additions & 0 deletions src/WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class WebSocketClient : public HttpClient
WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort = HttpClient::kHttpPort);
WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = HttpClient::kHttpPort);

/** Start the Web Socket connection to the specified path with headers
@param aPath Path to use in request
@param additionalHeaders 2D array with headers
@param headerRows amount of rows in additionalHeaders array
@return 0 if successful, else error
*/
int begin(const char* aPath, char* (*additionalHeaders)[2], size_t headerRows);
/** Start the Web Socket connection to the specified path
@param aURLPath Path to use in request (optional, "/" is used by default)
@return 0 if successful, else error
Expand Down