forked from timum-viw/socket.io-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketIoClient.h
43 lines (35 loc) · 1.3 KB
/
SocketIoClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef __SOCKET_IO_CLIENT_H__
#define __SOCKET_IO_CLIENT_H__
#include <Arduino.h>
#include <map>
#include <vector>
#include <WebSocketsClient.h>
#define SOCKETIOCLIENT_DEBUG(...) Serial.printf(__VA_ARGS__);
//#define SOCKETIOCLIENT_DEBUG(...)
#define PING_INTERVAL 10000 //TODO: use socket.io server response
//#define SOCKETIOCLIENT_USE_SSL
#ifdef SOCKETIOCLIENT_USE_SSL
#define DEFAULT_PORT 443
#else
#define DEFAULT_PORT 80
#endif
#define DEFAULT_URL "/socket.io/?transport=websocket"
#define DEFAULT_FINGERPRINT ""
class SocketIoClient {
private:
std::vector<String> _packets;
WebSocketsClient _webSocket;
int _lastPing;
std::map<String, std::function<void (const char * payload, size_t length)>> _events;
void trigger(const char* event, const char * payload, size_t length);
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
void initialize();
public:
void beginSSL(const char* host, const int port = DEFAULT_PORT, const char* url = DEFAULT_URL, const char* fingerprint = DEFAULT_FINGERPRINT);
void begin(const char* host, const int port = DEFAULT_PORT, const char* url = DEFAULT_URL);
void loop();
void on(const char* event, std::function<void (const char * payload, size_t length)>);
void emit(const char* event, const char * payload = NULL);
void disconnect();
};
#endif