WebSocketsClient.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file WebSocketsClient.h
  3. * @date 20.05.2015
  4. * @author Markus Sattler
  5. *
  6. * Copyright (c) 2015 Markus Sattler. All rights reserved.
  7. * This file is part of the WebSockets for Arduino.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #ifndef WEBSOCKETSCLIENT_H_
  25. #define WEBSOCKETSCLIENT_H_
  26. #include "WebSockets.h"
  27. class WebSocketsClient : protected WebSockets {
  28. public:
  29. #ifdef __AVR__
  30. typedef void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length);
  31. #else
  32. typedef std::function<void(WStype_t type, uint8_t * payload, size_t length)> WebSocketClientEvent;
  33. #endif
  34. WebSocketsClient(void);
  35. virtual ~WebSocketsClient(void);
  36. void begin(const char * host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
  37. void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");
  38. void begin(IPAddress host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
  39. #if defined(HAS_SSL)
  40. #ifdef SSL_AXTLS
  41. void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * fingerprint = "", const char * protocol = "arduino");
  42. void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
  43. #else
  44. void beginSSL(const char * host, uint16_t port, const char * url = "/", const uint8_t * fingerprint = NULL, const char * protocol = "arduino");
  45. void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
  46. void setSSLClientCertKey(BearSSL::X509List * clientCert = NULL, BearSSL::PrivateKey * clientPrivateKey = NULL);
  47. void setSSLClientCertKey(const char * clientCert = NULL, const char * clientPrivateKey = NULL);
  48. #endif
  49. void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
  50. #endif
  51. void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
  52. void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
  53. #if defined(HAS_SSL)
  54. void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
  55. void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
  56. void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
  57. #if defined(SSL_BARESSL)
  58. void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
  59. #endif
  60. #endif
  61. #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
  62. void loop(void);
  63. #else
  64. // Async interface not need a loop call
  65. void loop(void) __attribute__((deprecated)) {}
  66. #endif
  67. void onEvent(WebSocketClientEvent cbEvent);
  68. bool sendTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
  69. bool sendTXT(const uint8_t * payload, size_t length = 0);
  70. bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
  71. bool sendTXT(const char * payload, size_t length = 0);
  72. bool sendTXT(String & payload);
  73. bool sendTXT(char payload);
  74. bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
  75. bool sendBIN(const uint8_t * payload, size_t length);
  76. bool sendPing(uint8_t * payload = NULL, size_t length = 0);
  77. bool sendPing(String & payload);
  78. void disconnect(void);
  79. void setAuthorization(const char * user, const char * password);
  80. void setAuthorization(const char * auth);
  81. void setExtraHeaders(const char * extraHeaders = NULL);
  82. void setReconnectInterval(unsigned long time);
  83. void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
  84. void disableHeartbeat();
  85. bool isConnected(void);
  86. protected:
  87. String _host;
  88. uint16_t _port;
  89. #if defined(HAS_SSL)
  90. #ifdef SSL_AXTLS
  91. String _fingerprint;
  92. const char * _CA_cert;
  93. #define SSL_FINGERPRINT_IS_SET (_fingerprint.length())
  94. #define SSL_FINGERPRINT_NULL ""
  95. #else
  96. const uint8_t * _fingerprint;
  97. BearSSL::X509List * _CA_cert;
  98. BearSSL::X509List * _client_cert;
  99. BearSSL::PrivateKey * _client_key;
  100. #define SSL_FINGERPRINT_IS_SET (_fingerprint != NULL)
  101. #define SSL_FINGERPRINT_NULL NULL
  102. #endif
  103. #endif
  104. WSclient_t _client;
  105. WebSocketClientEvent _cbEvent;
  106. unsigned long _lastConnectionFail;
  107. unsigned long _reconnectInterval;
  108. unsigned long _lastHeaderSent;
  109. void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
  110. void clientDisconnect(WSclient_t * client);
  111. bool clientIsConnected(WSclient_t * client);
  112. #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
  113. void handleClientData(void);
  114. #endif
  115. void sendHeader(WSclient_t * client);
  116. void handleHeader(WSclient_t * client, String * headerLine);
  117. void connectedCb();
  118. void connectFailedCb();
  119. void handleHBPing(); // send ping in specified intervals
  120. #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
  121. void asyncConnect();
  122. #endif
  123. /**
  124. * called for sending a Event to the app
  125. * @param type WStype_t
  126. * @param payload uint8_t *
  127. * @param length size_t
  128. */
  129. virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
  130. if(_cbEvent) {
  131. _cbEvent(type, payload, length);
  132. }
  133. }
  134. };
  135. #endif /* WEBSOCKETSCLIENT_H_ */