/**************************************************************************************************************************** WiFi_WebSocketClient.h - Dead simple HTTP WebSockets Client. For any WiFi shields, such as WiFiNINA W101, W102, W13x, or custom, such as ESP8266/ESP32-AT, Ethernet, etc WiFiWebServer is a library for the ESP32-based WiFi shields to run WebServer Forked and modified from ESP8266 https://github.com/esp8266/Arduino/releases Forked and modified from Arduino WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA Built by Khoi Hoang https://github.com/khoih-prog/WiFiWebServer Licensed under MIT license Original author: @file Esp8266WebServer.h @author Ivan Grokhotkov Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 12/02/2020 Initial coding for SAMD21, Nano 33 IoT, etc running WiFiNINA ... 1.6.0 K Hoang 13/02/2022 Add support to new ESP32-S3 and ESP32_C3 1.6.1 K Hoang 13/02/2022 Fix v1.6.0 issue 1.6.2 K Hoang 22/02/2022 Add support to megaAVR using Arduino megaAVR core 1.6.3 K Hoang 02/03/2022 Fix decoding error bug 1.7.0 K Hoang 05/04/2022 Fix issue with Portenta_H7 core v2.7.2+ 1.8.0 K Hoang 26/04/2022 Add WiFiMulti library support and examples 1.9.0 K Hoang 12/08/2022 Add support to RASPBERRY_PI_PICO_W using CYW4343 WiFi 1.9.1 K Hoang 13/08/2022 Add WiFiMulti support to RASPBERRY_PI_PICO_W using CYW4343 WiFi 1.9.2 K Hoang 16/08/2022 Workaround for RP2040W WiFi.status() bug 1.9.3 K Hoang 16/08/2022 Better workaround for RP2040W WiFi.status() bug using ping() to local gateway 1.9.4 K Hoang 06/09/2022 Restore support to ESP32 and ESP8266 1.9.5 K Hoang 10/09/2022 Restore support to Teensy, etc. Fix bug in examples 1.10.0 K Hoang 13/11/2022 Add new features, such as CORS. Update code and examples 1.10.1 K Hoang 24/11/2022 Using new WiFi101_Generic library to send larger data *****************************************************************************************************************************/ // (c) Copyright Arduino. 2016 // Released under Apache License, version 2.0 #pragma once #ifndef WiFi_WebSocketClient_H #define WiFi_WebSocketClient_H #include #include "utility/WiFiDebug.h" #include "WiFi_HTTPClient/WiFi_HttpClient.h" //////////////////////////////////////// static const int TYPE_CONTINUATION = 0x0; static const int TYPE_TEXT = 0x1; static const int TYPE_BINARY = 0x2; static const int TYPE_CONNECTION_CLOSE = 0x8; static const int TYPE_PING = 0x9; static const int TYPE_PONG = 0xa; //////////////////////////////////////// class WiFiWebSocketClient : public WiFiHttpClient { public: WiFiWebSocketClient(Client& aClient, const char* aServerName, uint16_t aServerPort = WiFiHttpClient::kHttpPort); WiFiWebSocketClient(Client& aClient, const String& aServerName, uint16_t aServerPort = WiFiHttpClient::kHttpPort); WiFiWebSocketClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = WiFiHttpClient::kHttpPort); /** 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 */ int begin(const char* aPath = "/"); int begin(const String& aPath); /** Begin to send a message of type (TYPE_TEXT or TYPE_BINARY) Use the write or Stream API's to set message content, followed by endMessage to complete the message. @param aURLPath Path to use in request @return 0 if successful, else error */ int beginMessage(int aType); /** Completes sending of a message started by beginMessage @return 0 if successful, else error */ int endMessage(); /** Try to parse an incoming messages @return 0 if no message available, else size of parsed message */ int parseMessage(); /** Returns type of current parsed message @return type of current parsedMessage (TYPE_TEXT or TYPE_BINARY) */ int messageType(); /** Returns if the current message is the final chunk of a split message @return true for final message, false otherwise */ bool isFinal(); /** Read the current messages as a string @return current message as a string */ String readString(); /** Send a ping @return 0 if successful, else error */ int ping(); // Inherited from Print virtual size_t write(uint8_t aByte); virtual size_t write(const uint8_t *aBuffer, size_t aSize); // Inherited from Stream virtual int available(); /** Read the next byte from the server. @return Byte read or -1 if there are no bytes available. */ virtual int read(); virtual int read(uint8_t *buf, size_t size); virtual int peek(); private: void flushRx(); private: bool iTxStarted; uint8_t iTxMessageType; uint8_t iTxBuffer[128]; uint64_t iTxSize; uint8_t iRxOpCode; uint64_t iRxSize; bool iRxMasked; int iRxMaskIndex; uint8_t iRxMaskKey[4]; }; #endif // WiFi_WebSocketClient_H