|
6 | 6 | #include "WebSocketClient.h"
|
7 | 7 |
|
8 | 8 | WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
|
9 |
| - : HttpClient(aClient, aServerName, aServerPort) |
| 9 | + : HttpClient(aClient, aServerName, aServerPort), |
| 10 | + iRxSize(0) |
10 | 11 | {
|
11 | 12 | }
|
12 | 13 |
|
13 | 14 | WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
|
14 |
| - : HttpClient(aClient, aServerName, aServerPort) |
| 15 | + : HttpClient(aClient, aServerName, aServerPort), |
| 16 | + iRxSize(0) |
15 | 17 | {
|
16 | 18 | }
|
17 | 19 |
|
18 | 20 | WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
|
19 |
| - : HttpClient(aClient, aServerAddress, aServerPort) |
| 21 | + : HttpClient(aClient, aServerAddress, aServerPort), |
| 22 | + iRxSize(0) |
20 | 23 | {
|
21 | 24 | }
|
22 | 25 |
|
@@ -55,6 +58,8 @@ int WebSocketClient::begin(const char* aPath)
|
55 | 58 | }
|
56 | 59 | }
|
57 | 60 |
|
| 61 | + iRxSize = 0; |
| 62 | + |
58 | 63 | // status code of 101 means success
|
59 | 64 | return (status == 101) ? 0 : status;
|
60 | 65 | }
|
@@ -152,6 +157,8 @@ size_t WebSocketClient::write(const uint8_t *aBuffer, size_t aSize)
|
152 | 157 |
|
153 | 158 | int WebSocketClient::parseMessage()
|
154 | 159 | {
|
| 160 | + flushRx(); |
| 161 | + |
155 | 162 | // make sure 2 bytes (opcode + length)
|
156 | 163 | // are available
|
157 | 164 | if (HttpClient::available() < 2)
|
@@ -208,6 +215,29 @@ int WebSocketClient::parseMessage()
|
208 | 215 |
|
209 | 216 | iRxMaskIndex = 0;
|
210 | 217 |
|
| 218 | + if (TYPE_CONNECTION_CLOSE == messageType()) |
| 219 | + { |
| 220 | + flushRx(); |
| 221 | + stop(); |
| 222 | + iRxSize = 0; |
| 223 | + } |
| 224 | + else if (TYPE_PING == messageType()) |
| 225 | + { |
| 226 | + beginMessage(TYPE_PONG); |
| 227 | + while(available()) |
| 228 | + { |
| 229 | + write(read()); |
| 230 | + } |
| 231 | + endMessage(); |
| 232 | + |
| 233 | + iRxSize = 0; |
| 234 | + } |
| 235 | + else if (TYPE_PONG == messageType()) |
| 236 | + { |
| 237 | + flushRx(); |
| 238 | + iRxSize = 0; |
| 239 | + } |
| 240 | + |
211 | 241 | return iRxSize;
|
212 | 242 | }
|
213 | 243 |
|
@@ -239,6 +269,21 @@ String WebSocketClient::readString()
|
239 | 269 | return s;
|
240 | 270 | }
|
241 | 271 |
|
| 272 | +int WebSocketClient::ping() |
| 273 | +{ |
| 274 | + uint8_t pingData[16]; |
| 275 | + |
| 276 | + // create random data for the ping |
| 277 | + for (int i = 0; i < (int)sizeof(pingData); i++) |
| 278 | + { |
| 279 | + pingData[i] = random(0xff); |
| 280 | + } |
| 281 | + |
| 282 | + beginMessage(TYPE_PING); |
| 283 | + write(pingData, sizeof(pingData)); |
| 284 | + return endMessage(); |
| 285 | +} |
| 286 | + |
242 | 287 | int WebSocketClient::available()
|
243 | 288 | {
|
244 | 289 | if (iState < eReadingBody)
|
@@ -293,3 +338,11 @@ int WebSocketClient::peek()
|
293 | 338 |
|
294 | 339 | return p;
|
295 | 340 | }
|
| 341 | + |
| 342 | +void WebSocketClient::flushRx() |
| 343 | +{ |
| 344 | + while(available()) |
| 345 | + { |
| 346 | + read(); |
| 347 | + } |
| 348 | +} |
0 commit comments