1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-04-19 21:22:15 +03:00

Add ping, pong, and connection close support

This commit is contained in:
Sandeep Mistry 2016-06-29 13:04:27 -04:00
parent 1c56734360
commit 00cd68f52f
3 changed files with 65 additions and 3 deletions

View File

@ -6,17 +6,20 @@
#include "WebSocketClient.h" #include "WebSocketClient.h"
WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort) WebSocketClient::WebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
: HttpClient(aClient, aServerName, aServerPort) : HttpClient(aClient, aServerName, aServerPort),
iRxSize(0)
{ {
} }
WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort) WebSocketClient::WebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort)
: HttpClient(aClient, aServerName, aServerPort) : HttpClient(aClient, aServerName, aServerPort),
iRxSize(0)
{ {
} }
WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort) WebSocketClient::WebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
: HttpClient(aClient, aServerAddress, aServerPort) : HttpClient(aClient, aServerAddress, aServerPort),
iRxSize(0)
{ {
} }
@ -55,6 +58,8 @@ int WebSocketClient::begin(const char* aPath)
} }
} }
iRxSize = 0;
// status code of 101 means success // status code of 101 means success
return (status == 101) ? 0 : status; return (status == 101) ? 0 : status;
} }
@ -152,6 +157,8 @@ size_t WebSocketClient::write(const uint8_t *aBuffer, size_t aSize)
int WebSocketClient::parseMessage() int WebSocketClient::parseMessage()
{ {
flushRx();
// make sure 2 bytes (opcode + length) // make sure 2 bytes (opcode + length)
// are available // are available
if (HttpClient::available() < 2) if (HttpClient::available() < 2)
@ -208,6 +215,29 @@ int WebSocketClient::parseMessage()
iRxMaskIndex = 0; iRxMaskIndex = 0;
if (TYPE_CONNECTION_CLOSE == messageType())
{
flushRx();
stop();
iRxSize = 0;
}
else if (TYPE_PING == messageType())
{
beginMessage(TYPE_PONG);
while(available())
{
write(read());
}
endMessage();
iRxSize = 0;
}
else if (TYPE_PONG == messageType())
{
flushRx();
iRxSize = 0;
}
return iRxSize; return iRxSize;
} }
@ -239,6 +269,21 @@ String WebSocketClient::readString()
return s; return s;
} }
int WebSocketClient::ping()
{
uint8_t pingData[16];
// create random data for the ping
for (int i = 0; i < (int)sizeof(pingData); i++)
{
pingData[i] = random(0xff);
}
beginMessage(TYPE_PING);
write(pingData, sizeof(pingData));
return endMessage();
}
int WebSocketClient::available() int WebSocketClient::available()
{ {
if (iState < eReadingBody) if (iState < eReadingBody)
@ -293,3 +338,11 @@ int WebSocketClient::peek()
return p; return p;
} }
void WebSocketClient::flushRx()
{
while(available())
{
read();
}
}

View File

@ -63,6 +63,11 @@ public:
*/ */
String readString(); String readString();
/** Send a ping
@return 0 if successful, else error
*/
int ping();
// Inherited from Print // Inherited from Print
virtual size_t write(uint8_t aByte); virtual size_t write(uint8_t aByte);
virtual size_t write(const uint8_t *aBuffer, size_t aSize); virtual size_t write(const uint8_t *aBuffer, size_t aSize);
@ -75,6 +80,9 @@ public:
virtual int read(uint8_t *buf, size_t size); virtual int read(uint8_t *buf, size_t size);
virtual int peek(); virtual int peek();
private:
void flushRx();
private: private:
uint8_t iTxMessageType; uint8_t iTxMessageType;
uint8_t iTxBuffer[128]; uint8_t iTxBuffer[128];

View File

@ -42,6 +42,7 @@ parseMessage KEYWORD2
messageType KEYWORD2 messageType KEYWORD2
isFinal KEYWORD2 isFinal KEYWORD2
readString KEYWORD2 readString KEYWORD2
ping KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)