2015-05-22 14:19:01 +02:00
|
|
|
/**
|
|
|
|
|
* @file WebSocketsClient.h
|
|
|
|
|
* @date 20.05.2015
|
|
|
|
|
* @author Markus Sattler
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Markus Sattler. All rights reserved.
|
|
|
|
|
* This file is part of the WebSockets for Arduino.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef WEBSOCKETSCLIENT_H_
|
|
|
|
|
#define WEBSOCKETSCLIENT_H_
|
|
|
|
|
|
2015-05-23 23:51:32 +02:00
|
|
|
#include "WebSockets.h"
|
2015-05-22 14:19:01 +02:00
|
|
|
|
2019-06-10 13:23:15 +02:00
|
|
|
class WebSocketsClient : protected WebSockets {
|
2019-06-10 12:57:49 +02:00
|
|
|
public:
|
2016-01-29 13:44:18 +01:00
|
|
|
#ifdef __AVR__
|
2019-06-10 12:57:49 +02:00
|
|
|
typedef void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length);
|
2016-01-29 13:44:18 +01:00
|
|
|
#else
|
2019-06-10 12:57:49 +02:00
|
|
|
typedef std::function<void(WStype_t type, uint8_t * payload, size_t length)> WebSocketClientEvent;
|
2016-01-29 13:44:18 +01:00
|
|
|
#endif
|
|
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
WebSocketsClient(void);
|
|
|
|
|
virtual ~WebSocketsClient(void);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void begin(const char * host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
|
|
|
|
|
void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
|
|
|
|
|
void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-05-30 16:32:30 +02:00
|
|
|
#if defined(HAS_SSL)
|
2020-10-04 14:49:22 +02:00
|
|
|
#ifdef SSL_AXTLS
|
|
|
|
|
void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * fingerprint = "", const char * protocol = "arduino");
|
2019-06-10 12:57:49 +02:00
|
|
|
void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
|
2020-10-04 14:49:22 +02:00
|
|
|
#else
|
|
|
|
|
void beginSSL(const char * host, uint16_t port, const char * url = "/", const uint8_t * fingerprint = NULL, const char * protocol = "arduino");
|
|
|
|
|
void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
|
2020-10-26 18:01:31 +02:00
|
|
|
void setSSLClientCertKey(BearSSL::X509List * clientCert = NULL, BearSSL::PrivateKey * clientPrivateKey = NULL);
|
|
|
|
|
void setSSLClientCertKey(const char * clientCert = NULL, const char * clientPrivateKey = NULL);
|
2020-10-04 14:49:22 +02:00
|
|
|
#endif
|
2019-06-10 12:57:49 +02:00
|
|
|
void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
|
2015-12-10 10:10:06 +01:00
|
|
|
#endif
|
|
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
|
|
|
|
void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
2016-06-06 15:21:13 +03:00
|
|
|
|
2019-05-30 16:32:30 +02:00
|
|
|
#if defined(HAS_SSL)
|
2019-06-10 12:57:49 +02:00
|
|
|
void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
|
|
|
|
void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
2020-10-26 18:01:31 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
|
2020-10-26 18:01:31 +02:00
|
|
|
#if defined(SSL_BARESSL)
|
|
|
|
|
void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
|
2017-06-18 12:51:34 +02:00
|
|
|
#endif
|
2020-10-26 18:01:31 +02:00
|
|
|
#endif
|
2020-10-26 17:04:19 +01:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
|
|
|
|
void loop(void);
|
2016-01-24 00:52:23 +01:00
|
|
|
#else
|
2019-06-10 12:57:49 +02:00
|
|
|
// Async interface not need a loop call
|
|
|
|
|
void loop(void) __attribute__((deprecated)) {}
|
2016-01-24 00:52:23 +01:00
|
|
|
#endif
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void onEvent(WebSocketClientEvent cbEvent);
|
|
|
|
|
|
|
|
|
|
bool sendTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
|
|
|
|
|
bool sendTXT(const uint8_t * payload, size_t length = 0);
|
|
|
|
|
bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
|
|
|
|
|
bool sendTXT(const char * payload, size_t length = 0);
|
|
|
|
|
bool sendTXT(String & payload);
|
2019-06-10 13:23:15 +02:00
|
|
|
bool sendTXT(char payload);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
|
|
|
|
|
bool sendBIN(const uint8_t * payload, size_t length);
|
2016-02-20 12:27:19 +01:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
bool sendPing(uint8_t * payload = NULL, size_t length = 0);
|
|
|
|
|
bool sendPing(String & payload);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void disconnect(void);
|
2016-10-22 19:47:44 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void setAuthorization(const char * user, const char * password);
|
|
|
|
|
void setAuthorization(const char * auth);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void setExtraHeaders(const char * extraHeaders = NULL);
|
2016-02-17 17:56:03 +01:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void setReconnectInterval(unsigned long time);
|
2017-08-19 21:16:07 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
|
|
|
|
|
void disableHeartbeat();
|
2018-10-23 18:01:33 +02:00
|
|
|
|
2020-07-11 17:58:36 +02:00
|
|
|
bool isConnected(void);
|
|
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
protected:
|
|
|
|
|
String _host;
|
|
|
|
|
uint16_t _port;
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-05-30 16:32:30 +02:00
|
|
|
#if defined(HAS_SSL)
|
2020-10-04 14:49:22 +02:00
|
|
|
#ifdef SSL_AXTLS
|
2019-06-10 12:57:49 +02:00
|
|
|
String _fingerprint;
|
|
|
|
|
const char * _CA_cert;
|
2021-03-06 09:22:52 +01:00
|
|
|
#define SSL_FINGERPRINT_IS_SET (_fingerprint.length())
|
2020-10-04 14:49:22 +02:00
|
|
|
#define SSL_FINGERPRINT_NULL ""
|
|
|
|
|
#else
|
|
|
|
|
const uint8_t * _fingerprint;
|
|
|
|
|
BearSSL::X509List * _CA_cert;
|
2020-10-26 18:01:31 +02:00
|
|
|
BearSSL::X509List * _client_cert;
|
|
|
|
|
BearSSL::PrivateKey * _client_key;
|
2021-03-06 09:22:52 +01:00
|
|
|
#define SSL_FINGERPRINT_IS_SET (_fingerprint != NULL)
|
2020-10-04 14:49:22 +02:00
|
|
|
#define SSL_FINGERPRINT_NULL NULL
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-12-24 12:58:05 +01:00
|
|
|
#endif
|
2019-06-10 12:57:49 +02:00
|
|
|
WSclient_t _client;
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
WebSocketClientEvent _cbEvent;
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
unsigned long _lastConnectionFail;
|
|
|
|
|
unsigned long _reconnectInterval;
|
2020-09-12 17:20:03 +02:00
|
|
|
unsigned long _lastHeaderSent;
|
2017-08-19 21:16:07 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void clientDisconnect(WSclient_t * client);
|
|
|
|
|
bool clientIsConnected(WSclient_t * client);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
#if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
|
|
|
|
|
void handleClientData(void);
|
2016-01-23 16:27:02 +01:00
|
|
|
#endif
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void sendHeader(WSclient_t * client);
|
|
|
|
|
void handleHeader(WSclient_t * client, String * headerLine);
|
2015-05-23 23:51:32 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void connectedCb();
|
|
|
|
|
void connectFailedCb();
|
2016-01-24 00:52:23 +01:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
void handleHBPing(); // send ping in specified intervals
|
2018-10-23 18:01:33 +02:00
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
#if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
|
|
|
|
|
void asyncConnect();
|
2016-01-24 00:52:23 +01:00
|
|
|
#endif
|
|
|
|
|
|
2019-06-10 12:57:49 +02:00
|
|
|
/**
|
2015-10-31 11:37:07 +01:00
|
|
|
* called for sending a Event to the app
|
|
|
|
|
* @param type WStype_t
|
|
|
|
|
* @param payload uint8_t *
|
|
|
|
|
* @param length size_t
|
|
|
|
|
*/
|
2019-06-10 12:57:49 +02:00
|
|
|
virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|
|
|
|
if(_cbEvent) {
|
|
|
|
|
_cbEvent(type, payload, length);
|
2015-10-31 11:37:07 +01:00
|
|
|
}
|
2019-06-10 12:57:49 +02:00
|
|
|
}
|
2015-05-23 23:51:32 +02:00
|
|
|
};
|
2015-05-22 14:19:01 +02:00
|
|
|
|
|
|
|
|
#endif /* WEBSOCKETSCLIENT_H_ */
|