@@ -20,6 +20,19 @@ License along with this library; if not, write to the Free Software
20
20
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
21
*/
22
22
23
+ /* **** Note Regarding Client Connection Keeping ****
24
+ Client connection is established in functions that directly involve use of client,
25
+ i.e sendGetToTelegram, sendPostToTelegram, and sendMultipartFormDataToTelegram.
26
+ It is closed at the end of sendMultipartFormDataToTelegram, but not at the
27
+ end of sendGetToTelegram and sendPostToTelegram as these may need to keep the
28
+ connection alive for respose / response checking. Re-establishing a connection
29
+ then wastes time which is noticeable in user experience.
30
+ Due to this, it is important that connection be closed manually after calling
31
+ sendGetToTelegram or sendPostToTelegram by calling closeClient();
32
+ Failure to close connection causes memory leakage, and SSL errors on ESP32
33
+ Note: closeClient() only closes connections on ESP32 (#ifdef ESP32).
34
+ */
35
+
23
36
#include " UniversalTelegramBot.h"
24
37
25
38
UniversalTelegramBot::UniversalTelegramBot (String token, Client &client) {
@@ -32,8 +45,14 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
32
45
long now;
33
46
bool avail;
34
47
35
- // Connect with api.telegram.org
36
- if (client->connect (HOST, SSL_PORT)) {
48
+ // Connect with api.telegram.org if not already connected
49
+ if (!client->connected ()){
50
+ if (_debug) Serial.println (F (" [BOT]Connecting to server" ));
51
+ if (!client->connect (HOST, SSL_PORT)){
52
+ if (_debug) Serial.println (F (" [BOT]Conection error" ));
53
+ }
54
+ }
55
+ if (client->connected ()) {
37
56
38
57
if (_debug) Serial.println (F (" .... connected to server" ));
39
58
@@ -74,8 +93,14 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
74
93
long now;
75
94
bool responseReceived;
76
95
77
- // Connect with api.telegram.org
78
- if (client->connect (HOST, SSL_PORT)) {
96
+ // Connect with api.telegram.org if not already connected
97
+ if (!client->connected ()){
98
+ if (_debug) Serial.println (F (" [BOT Client]Connecting to server" ));
99
+ if (!client->connect (HOST, SSL_PORT)){
100
+ if (_debug) Serial.println (F (" [BOT Client]Conection error" ));
101
+ }
102
+ }
103
+ if (client->connected ()) {
79
104
// POST URI
80
105
client->print (" POST /" + command); client->println (F (" HTTP/1.1" ));
81
106
// Host header
@@ -153,8 +178,15 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, Str
153
178
long now;
154
179
bool responseReceived;
155
180
String boundry = F (" ------------------------b8f610217e83e29b" );
156
- // Connect with api.telegram.org
157
- if (client->connect (HOST, SSL_PORT)) {
181
+
182
+ // Connect with api.telegram.org if not already connected
183
+ if (!client->connected ()){
184
+ if (_debug) Serial.println (F (" [BOT Client]Connecting to server" ));
185
+ if (!client->connect (HOST, SSL_PORT)){
186
+ if (_debug) Serial.println (F (" [BOT Client]Conection error" ));
187
+ }
188
+ }
189
+ if (client->connected ()) {
158
190
159
191
String start_request = " " ;
160
192
String end_request = " " ;
@@ -262,6 +294,7 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, Str
262
294
}
263
295
}
264
296
297
+ closeClient ();
265
298
return body;
266
299
}
267
300
@@ -271,6 +304,8 @@ bool UniversalTelegramBot::getMe() {
271
304
DynamicJsonBuffer jsonBuffer;
272
305
JsonObject& root = jsonBuffer.parseObject (response);
273
306
307
+ closeClient ();
308
+
274
309
if (root.success ()) {
275
310
if (root.containsKey (" result" )) {
276
311
String _name = root[" result" ][" first_name" ];
@@ -301,6 +336,8 @@ int UniversalTelegramBot::getUpdates(long offset) {
301
336
302
337
if (response == " " ) {
303
338
if (_debug) Serial.println (F (" Received empty string in response!" ));
339
+ // close the client as there's nothing to do with an empty string
340
+ closeClient ();
304
341
return 0 ;
305
342
}
306
343
else {
@@ -328,18 +365,25 @@ int UniversalTelegramBot::getUpdates(long offset) {
328
365
newMessageIndex++;
329
366
}
330
367
}
368
+ // We will keep the client open because there may be a response to be given
331
369
return newMessageIndex;
332
370
} else {
333
371
if (_debug) Serial.println (F (" no new messages" ));
334
372
}
335
373
} else {
336
374
if (_debug) Serial.println (F (" Response contained no 'result'" ));
337
375
}
338
- } else {
339
- // Buffer may not be big enough, increase buffer or reduce max number of messages
340
- if (_debug) Serial.println (F (" Failed to parse update, the message could be too big for the buffer" ));
376
+ } else { // Parsing failed
377
+ if (response.length () < 2 ){ // Too short a message. Maybe connection issue
378
+ if (_debug) Serial.println (F (" Parsing error: Message too short" ));
379
+ }
380
+ else {
381
+ // Buffer may not be big enough, increase buffer or reduce max number of messages
382
+ if (_debug) Serial.println (F (" Failed to parse update, the message could be too big for the buffer" ));
383
+ }
341
384
}
342
-
385
+ // Close the client as no response is to be given
386
+ closeClient ();
343
387
return 0 ;
344
388
}
345
389
}
@@ -441,7 +485,7 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String
441
485
}
442
486
}
443
487
}
444
-
488
+ closeClient ();
445
489
return sent;
446
490
}
447
491
@@ -540,6 +584,7 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
540
584
}
541
585
}
542
586
587
+ closeClient ();
543
588
return sent;
544
589
}
545
590
@@ -562,6 +607,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
562
607
}
563
608
}
564
609
610
+ closeClient ();
565
611
return response;
566
612
}
567
613
@@ -642,5 +688,15 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
642
688
}
643
689
}
644
690
691
+ closeClient ();
645
692
return sent;
646
693
}
694
+
695
+ void UniversalTelegramBot::closeClient (){
696
+ #ifdef ESP32
697
+ if (client->connected ()){
698
+ if (_debug){Serial.println (F (" Closing client" )); }
699
+ client->stop ();
700
+ }
701
+ #endif
702
+ }
0 commit comments