Skip to content

Commit 4a66056

Browse files
committed
Support additional headers in WebSocketClient
1 parent 1751e10 commit 4a66056

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

Diff for: src/WebSocketClient.cpp

+20-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint1
1212
{
1313
}
1414

15-
WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
15+
WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
1616
: HttpClient(aClient, aServerName, aServerPort),
1717
iTxStarted(false),
1818
iRxSize(0)
@@ -26,7 +26,7 @@ WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddres
2626
{
2727
}
2828

29-
int WebSocketClient::begin(const char* aPath)
29+
int WebSocketClient::begin(const char* aPath, char* (*additionalHeaders)[2], size_t headerRows)
3030
{
3131
// start the GET request
3232
beginRequest();
@@ -51,6 +51,10 @@ int WebSocketClient::begin(const char* aPath)
5151
sendHeader("Connection", "Upgrade");
5252
sendHeader("Sec-WebSocket-Key", base64RandomKey);
5353
sendHeader("Sec-WebSocket-Version", "13");
54+
for (size_t i = 0; i < headerRows; ++i)
55+
{
56+
sendHeader(additionalHeaders[i][0], additionalHeaders[i][1]);
57+
}
5458
endRequest();
5559

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

74+
int WebSocketClient::begin(const char* aPath)
75+
{
76+
return begin(aPath, NULL, 0);
77+
}
78+
7079
int WebSocketClient::begin(const String& aPath)
7180
{
72-
return begin(aPath.c_str());
81+
return begin(aPath.c_str(), NULL, 0);
7382
}
7483

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

176185
iTxSize += aSize;
177-
186+
178187
return aSize;
179188
}
180189

@@ -217,14 +226,14 @@ int WebSocketClient::parseMessage()
217226
}
218227
else
219228
{
220-
iRxSize = ((uint64_t)HttpClient::read() << 56) |
221-
((uint64_t)HttpClient::read() << 48) |
222-
((uint64_t)HttpClient::read() << 40) |
223-
((uint64_t)HttpClient::read() << 32) |
224-
((uint64_t)HttpClient::read() << 24) |
225-
((uint64_t)HttpClient::read() << 16) |
229+
iRxSize = ((uint64_t)HttpClient::read() << 56) |
230+
((uint64_t)HttpClient::read() << 48) |
231+
((uint64_t)HttpClient::read() << 40) |
232+
((uint64_t)HttpClient::read() << 32) |
233+
((uint64_t)HttpClient::read() << 24) |
234+
((uint64_t)HttpClient::read() << 16) |
226235
((uint64_t)HttpClient::read() << 8) |
227-
(uint64_t)HttpClient::read();
236+
(uint64_t)HttpClient::read();
228237
}
229238

230239
// read in the mask, if present

Diff for: src/WebSocketClient.h

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class WebSocketClient : public HttpClient
2222
WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort = HttpClient::kHttpPort);
2323
WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = HttpClient::kHttpPort);
2424

25+
/** Start the Web Socket connection to the specified path with headers
26+
@param aPath Path to use in request
27+
@param additionalHeaders 2D array with headers
28+
@param headerRows amound of rows in additionalHeaders array
29+
@return 0 if successful, else error
30+
*/
31+
int begin(const char* aPath, char* (*additionalHeaders)[2], size_t headerRows);
2532
/** Start the Web Socket connection to the specified path
2633
@param aURLPath Path to use in request (optional, "/" is used by default)
2734
@return 0 if successful, else error

0 commit comments

Comments
 (0)