WebSocketsClient.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. void beginSSL(const char * host, uint16_t port, const char * url = "/", const char * = "", const char * protocol = "arduino");
  41. void beginSSL(String host, uint16_t port, String url = "/", String fingerprint = "", String protocol = "arduino");
  42. void beginSslWithCA(const char * host, uint16_t port, const char * url = "/", const char * CA_cert = NULL, const char * protocol = "arduino");
  43. #endif
  44. void beginSocketIO(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
  45. void beginSocketIO(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
  46. #if defined(HAS_SSL)
  47. void beginSocketIOSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
  48. void beginSocketIOSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
  49. void beginSocketIOSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
  50. #endif
  51. #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
  52. void loop(void);
  53. #else
  54. // Async interface not need a loop call
  55. void loop(void) __attribute__((deprecated)) {}
  56. #endif
  57. void onEvent(WebSocketClientEvent cbEvent);
  58. bool sendTXT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
  59. bool sendTXT(const uint8_t * payload, size_t length = 0);
  60. bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
  61. bool sendTXT(const char * payload, size_t length = 0);
  62. bool sendTXT(String & payload);
  63. bool sendTXT(char payload);
  64. bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
  65. bool sendBIN(const uint8_t * payload, size_t length);
  66. bool sendPing(uint8_t * payload = NULL, size_t length = 0);
  67. bool sendPing(String & payload);
  68. void disconnect(void);
  69. void setAuthorization(const char * user, const char * password);
  70. void setAuthorization(const char * auth);
  71. void setExtraHeaders(const char * extraHeaders = NULL);
  72. void setReconnectInterval(unsigned long time);
  73. void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
  74. void disableHeartbeat();
  75. protected:
  76. String _host;
  77. uint16_t _port;
  78. bool isConnected(void);
  79. #if defined(HAS_SSL)
  80. String _fingerprint;
  81. const char * _CA_cert;
  82. #endif
  83. WSclient_t _client;
  84. WebSocketClientEvent _cbEvent;
  85. unsigned long _lastConnectionFail;
  86. unsigned long _reconnectInterval;
  87. void messageReceived(WSclient_t * client, WSopcode_t opcode, uint8_t * payload, size_t length, bool fin);
  88. void clientDisconnect(WSclient_t * client);
  89. bool clientIsConnected(WSclient_t * client);
  90. #if(WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
  91. void handleClientData(void);
  92. #endif
  93. void sendHeader(WSclient_t * client);
  94. void handleHeader(WSclient_t * client, String * headerLine);
  95. void connectedCb();
  96. void connectFailedCb();
  97. void handleHBPing(); // send ping in specified intervals
  98. #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
  99. void asyncConnect();
  100. #endif
  101. /**
  102. * called for sending a Event to the app
  103. * @param type WStype_t
  104. * @param payload uint8_t *
  105. * @param length size_t
  106. */
  107. virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
  108. if(_cbEvent) {
  109. _cbEvent(type, payload, length);
  110. }
  111. }
  112. };
  113. #endif /* WEBSOCKETSCLIENT_H_ */