Skip to content

Commit 40c37b9

Browse files
authored
Fix EventSource ::send (me-no-dev#1)
1 parent ae1258f commit 40c37b9

File tree

3 files changed

+74
-72
lines changed

3 files changed

+74
-72
lines changed

src/AsyncEventSource.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,16 @@ size_t AsyncEventSourceMessage::ack(size_t len, uint32_t time) {
138138
}
139139

140140
size_t AsyncEventSourceMessage::send(AsyncClient *client) {
141+
if (!client->canSend())
142+
return 0;
141143
const size_t len = _len - _sent;
142144
if(client->space() < len){
143145
return 0;
144146
}
145147
size_t sent = client->add((const char *)_data, len);
146-
if(client->canSend())
147-
client->send();
148+
client->send();
148149
_sent += sent;
149-
return sent;
150+
return sent;
150151
}
151152

152153
// Client
@@ -159,7 +160,7 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
159160
_lastId = 0;
160161
if(request->hasHeader("Last-Event-ID"))
161162
_lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str());
162-
163+
163164
_client->setRxTimeout(0);
164165
_client->onError(NULL, NULL);
165166
_client->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ (void)c; ((AsyncEventSourceClient*)(r))->_onAck(len, time); }, this);
@@ -276,7 +277,7 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
276277
client->write((const char *)temp, 2053);
277278
free(temp);
278279
}*/
279-
280+
280281
_clients.add(client);
281282
if(_connectcb)
282283
_connectcb(client);
@@ -297,10 +298,10 @@ void AsyncEventSource::close(){
297298
size_t AsyncEventSource::avgPacketsWaiting() const {
298299
if(_clients.isEmpty())
299300
return 0;
300-
301+
301302
size_t aql=0;
302303
uint32_t nConnectedClients=0;
303-
304+
304305
for(const auto &c: _clients){
305306
if(c->connected()) {
306307
aql+=c->packetsWaiting();

src/AsyncEventSource.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
#ifndef ASYNCEVENTSOURCE_H_
2121
#define ASYNCEVENTSOURCE_H_
2222

23+
#include <Arduino.h>
2324
#include <Arduino.h>
2425
#ifdef ESP32
2526
#include <AsyncTCP.h>
26-
#define SSE_MAX_QUEUED_MESSAGES 32
2727
#else
2828
#include <ESPAsyncTCP.h>
29-
#define SSE_MAX_QUEUED_MESSAGES 8
3029
#endif
30+
31+
#ifndef SSE_MAX_QUEUED_MESSAGES
32+
#define SSE_MAX_QUEUED_MESSAGES 32
33+
#endif
34+
3135
#include <ESPAsyncWebServer.h>
3236

3337
#include "AsyncWebSynchronization.h"
@@ -52,11 +56,11 @@ typedef std::function<void(AsyncEventSourceClient *client)> ArEventHandlerFuncti
5256

5357
class AsyncEventSourceMessage {
5458
private:
55-
uint8_t * _data;
59+
uint8_t * _data;
5660
size_t _len;
5761
size_t _sent;
5862
//size_t _ack;
59-
size_t _acked;
63+
size_t _acked;
6064
public:
6165
AsyncEventSourceMessage(const char * data, size_t len);
6266
~AsyncEventSourceMessage();
@@ -90,7 +94,7 @@ class AsyncEventSourceClient {
9094

9195
//system callbacks (do not call)
9296
void _onAck(size_t len, uint32_t time);
93-
void _onPoll();
97+
void _onPoll();
9498
void _onTimeout(uint32_t time);
9599
void _onDisconnect();
96100
};

0 commit comments

Comments
 (0)