SocketIOclient.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * SocketIOclient.h
  3. *
  4. * Created on: May 12, 2018
  5. * Author: links
  6. */
  7. #ifndef SOCKETIOCLIENT_H_
  8. #define SOCKETIOCLIENT_H_
  9. #include "WebSockets.h"
  10. #define EIO_HEARTBEAT_INTERVAL 20000
  11. #define EIO_MAX_HEADER_SIZE (WEBSOCKETS_MAX_HEADER_SIZE + 1)
  12. #define SIO_MAX_HEADER_SIZE (EIO_MAX_HEADER_SIZE + 1)
  13. typedef enum {
  14. eIOtype_OPEN = '0', ///< Sent from the server when a new transport is opened (recheck)
  15. eIOtype_CLOSE = '1', ///< Request the close of this transport but does not shutdown the connection itself.
  16. eIOtype_PING = '2', ///< Sent by the client. Server should answer with a pong packet containing the same data
  17. eIOtype_PONG = '3', ///< Sent by the server to respond to ping packets.
  18. eIOtype_MESSAGE = '4', ///< actual message, client and server should call their callbacks with the data
  19. eIOtype_UPGRADE = '5', ///< Before engine.io switches a transport, it tests, if server and client can communicate over this transport. If this test succeed, the client sends an upgrade packets which requests the server to flush its cache on the old transport and switch to the new transport.
  20. eIOtype_NOOP = '6', ///< A noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received.
  21. } engineIOmessageType_t;
  22. typedef enum {
  23. sIOtype_CONNECT = '0',
  24. sIOtype_DISCONNECT = '1',
  25. sIOtype_EVENT = '2',
  26. sIOtype_ACK = '3',
  27. sIOtype_ERROR = '4',
  28. sIOtype_BINARY_EVENT = '5',
  29. sIOtype_BINARY_ACK = '6',
  30. } socketIOmessageType_t;
  31. class SocketIOclient : protected WebSocketsClient {
  32. public:
  33. #ifdef __AVR__
  34. typedef void (*SocketIOclientEvent)(socketIOmessageType_t type, uint8_t * payload, size_t length);
  35. #else
  36. typedef std::function<void(socketIOmessageType_t type, uint8_t * payload, size_t length)> SocketIOclientEvent;
  37. #endif
  38. SocketIOclient(void);
  39. virtual ~SocketIOclient(void);
  40. void begin(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
  41. void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
  42. bool isConnected(void);
  43. void onEvent(SocketIOclientEvent cbEvent);
  44. bool sendEVENT(uint8_t * payload, size_t length = 0, bool headerToPayload = false);
  45. bool sendEVENT(const uint8_t * payload, size_t length = 0);
  46. bool sendEVENT(char * payload, size_t length = 0, bool headerToPayload = false);
  47. bool sendEVENT(const char * payload, size_t length = 0);
  48. bool sendEVENT(String & payload);
  49. void loop(void);
  50. protected:
  51. uint64_t _lastHeartbeat = 0;
  52. SocketIOclientEvent _cbEvent;
  53. virtual void runIOCbEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
  54. if(_cbEvent) {
  55. _cbEvent(type, payload, length);
  56. }
  57. }
  58. // Handeling events from websocket layer
  59. virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
  60. handleCbEvent(type, payload, length);
  61. }
  62. void handleCbEvent(WStype_t type, uint8_t * payload, size_t length);
  63. };
  64. #endif /* SOCKETIOCLIENT_H_ */