-
Notifications
You must be signed in to change notification settings - Fork 4
/
Websocket.cpp
199 lines (161 loc) · 5.57 KB
/
Websocket.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Websocket.h"
#if defined(ARDUINO)
void WebsocketClient::enableHeartbeat(unsigned long interval, unsigned long timeout, uint8_t maxMissed) {
WebSocketsClient::enableHeartbeat(interval, timeout, maxMissed);
}
void WebsocketClient::disableHeartbeat() {
WebSocketsClient::disableHeartbeat();
}
void WebsocketClient::setReconnectInterval(unsigned long interval) {
WebSocketsClient::setReconnectInterval(interval);
}
void WebsocketClient::poll() {
WebSocketsClient::loop();
}
void WebsocketClient::onEvent(WebSocketEventCallback callback) {
WS_DEBUG("Setting event callback\n");
this->eventCallback = callback;
}
void WebsocketClient::connect(WSInterfaceString host, int port, WSInterfaceString path) {
WS_DEBUG("Connecting to ws://%s:%d%s\n", host.c_str(), port, path.c_str());
WebSocketsClient::begin(host, port, path);
}
void WebsocketClient::connectSecure(WSInterfaceString host, int port, WSInterfaceString path) {
WebSocketsClient::beginSSL(host.c_str(), port, path.c_str());
}
bool WebsocketClient::stream() {
if (isStreaming) {
WS_DEBUG("Already streaming\n");
return false;
}
if (clientIsConnected(&_client)) {
isStreaming = sendFrame(&_client, WSop_text, (uint8_t *) "", 0, false, false);
} else {
isStreaming = false;
}
return isStreaming;
}
bool WebsocketClient::send(uint8_t *payload, size_t length, bool headerToPayload) {
WS_DEBUG("Sending message of length %d\n", length);
if (length == 0) {
length = strlen((const char *) payload);
}
if (clientIsConnected(&_client)) {
if (isStreaming) {
return sendFrame(&_client, WSop_continuation, payload, length, false, headerToPayload);
} else {
return sendFrame(&_client, WSop_text, payload, length, true, headerToPayload);
}
}
return false;
}
bool WebsocketClient::send(const char *payload, size_t length, bool headerToPayload) {
return send((uint8_t *) payload, length, headerToPayload);
}
bool WebsocketClient::end() {
if (!isStreaming) {
return true;
}
WS_DEBUG("Ending stream\n");
bool res = sendFrame(&_client, WSop_continuation, (uint8_t *) "", 0, true, false);
isStreaming = !res;
return res;
}
#else
void WebsocketClient::enableHeartbeat(unsigned long interval, unsigned long timeout, uint8_t maxMissed) {
heartbeatEnabled = true;
heartbeatInterval = interval;
heartbeatTimeout = timeout;
heartbeatMaxMissed = maxMissed;
}
void WebsocketClient::disableHeartbeat() {
heartbeatEnabled = false;
}
void WebsocketClient::setReconnectInterval(unsigned long interval) {
reconnectInterval = interval;
}
unsigned long millis() {
struct timeval tv;
uint64_t now;
gettimeofday(&tv, NULL);
return now = (uint64_t) tv.tv_sec * (uint64_t) 1000 + (uint64_t) (tv.tv_usec / 1000);
}
void WebsocketClient::poll() {
websockets::WebsocketsClient::poll();
if (heartbeatEnabled && available()) {
if (!heartbeatInProgress && (millis() - heartbeatLastSent > heartbeatInterval)) {
if (heartbeatMissed >= heartbeatMaxMissed) {
// Too many missed heartbeats, close the connection
WS_DEBUG("Too many missed heartbeats, closing connection\n");
reconnectLastAttempt = 0;
heartbeatMissed = 0;
websockets::WebsocketsClient::close();
return;
}
WS_DEBUG("Sending ping\n");
ping();
heartbeatLastSent = millis();
heartbeatInProgress = true;
}
if (heartbeatInProgress && (millis() - heartbeatLastSent > heartbeatTimeout)) {
// Heartbeat timeout
WS_DEBUG("Heartbeat timeout\n");
heartbeatMissed++;
heartbeatInProgress = false;
return;
}
}
if (shouldReconnect && !available()) {
if (millis() - reconnectLastAttempt > reconnectInterval) {
WS_DEBUG("Reconnecting...\n");
// Attempt to reconnect
websockets::WebsocketsClient::connect(host, port, path);
WS_DEBUG("Reconnect attempt complete\n");
WS_DEBUG("Connection status: %d\n", websockets::WebsocketsClient::available());
reconnectLastAttempt = millis();
}
}
}
void WebsocketClient::onEvent(WebSocketEventCallback callback) {
WS_DEBUG("Setting event callback\n");
this->eventCallback = callback;
}
void WebsocketClient::connect(WSInterfaceString host, int port, WSInterfaceString path) {
WS_DEBUG("Connecting to ws://%s:%d%s\n", host, port, path);
this->host = host;
this->port = port;
this->path = path;
shouldReconnect = true;
heartbeatMissed = 0;
heartbeatInProgress = false;
// isSecure = false;
websockets::WebsocketsClient::connect(this->host.c_str(), this->port, this->path.c_str());
}
// void WebsocketClient::connectSecure(WSInterfaceString host, int port, WSInterfaceString path) {
// WS_DEBUG("Connecting to wss://%s:%d%s\n", host.c_str(), port, path.c_str());
// this->host = host;
// this->port = port;
// this->path = path;
// shouldReconnect = true;
// heartbeatMissed = 0;
// heartbeatInProgress = false;
// isSecure = true;
// websockets::WebsocketsClient::connect(this->host.c_str(), this->port, this->path.c_str());
// }
bool WebsocketClient::stream() {
return websockets::WebsocketsClient::stream();
}
bool WebsocketClient::send(uint8_t *payload, size_t length, bool headerToPayload) {
return send((const char *) payload, length, headerToPayload);
}
bool WebsocketClient::send(const char *payload, size_t length, bool headerToPayload) {
WS_DEBUG("Sending message of length %d\n", length);
if (length == 0) {
length = strlen(payload);
}
return websockets::WebsocketsClient::send((const char *) payload, length);
}
bool WebsocketClient::end() {
return websockets::WebsocketsClient::end();
}
#endif