You've already forked ArduinoHttpClient
							
							
				mirror of
				https://github.com/arduino-libraries/ArduinoHttpClient.git
				synced 2025-10-31 06:50:28 +03:00 
			
		
		
		
	Add ping, pong, and connection close support
This commit is contained in:
		| @@ -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(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
| @@ -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]; | ||||||
|   | |||||||
| @@ -42,6 +42,7 @@ parseMessage	KEYWORD2 | |||||||
| messageType	KEYWORD2 | messageType	KEYWORD2 | ||||||
| isFinal	KEYWORD2 | isFinal	KEYWORD2 | ||||||
| readString	KEYWORD2 | readString	KEYWORD2 | ||||||
|  | ping	KEYWORD2 | ||||||
|  |  | ||||||
| ####################################### | ####################################### | ||||||
| # Constants (LITERAL1) | # Constants (LITERAL1) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user