Skip to content

Fix EventSource ::send #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/AsyncEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,16 @@ size_t AsyncEventSourceMessage::ack(size_t len, uint32_t time) {
}

size_t AsyncEventSourceMessage::send(AsyncClient *client) {
if (!client->canSend())
return 0;
const size_t len = _len - _sent;
if(client->space() < len){
return 0;
}
size_t sent = client->add((const char *)_data, len);
if(client->canSend())
client->send();
client->send();
_sent += sent;
return sent;
return sent;
}

// Client
Expand All @@ -159,7 +160,7 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
_lastId = 0;
if(request->hasHeader("Last-Event-ID"))
_lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str());

_client->setRxTimeout(0);
_client->onError(NULL, NULL);
_client->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ (void)c; ((AsyncEventSourceClient*)(r))->_onAck(len, time); }, this);
Expand Down Expand Up @@ -276,7 +277,7 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
client->write((const char *)temp, 2053);
free(temp);
}*/

_clients.add(client);
if(_connectcb)
_connectcb(client);
Expand All @@ -297,10 +298,10 @@ void AsyncEventSource::close(){
size_t AsyncEventSource::avgPacketsWaiting() const {
if(_clients.isEmpty())
return 0;

size_t aql=0;
uint32_t nConnectedClients=0;

for(const auto &c: _clients){
if(c->connected()) {
aql+=c->packetsWaiting();
Expand Down
14 changes: 9 additions & 5 deletions src/AsyncEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
#ifndef ASYNCEVENTSOURCE_H_
#define ASYNCEVENTSOURCE_H_

#include <Arduino.h>
#include <Arduino.h>
#ifdef ESP32
#include <AsyncTCP.h>
#define SSE_MAX_QUEUED_MESSAGES 32
#else
#include <ESPAsyncTCP.h>
#define SSE_MAX_QUEUED_MESSAGES 8
#endif

#ifndef SSE_MAX_QUEUED_MESSAGES
#define SSE_MAX_QUEUED_MESSAGES 32
#endif

#include <ESPAsyncWebServer.h>

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

class AsyncEventSourceMessage {
private:
uint8_t * _data;
uint8_t * _data;
size_t _len;
size_t _sent;
//size_t _ack;
size_t _acked;
size_t _acked;
public:
AsyncEventSourceMessage(const char * data, size_t len);
~AsyncEventSourceMessage();
Expand Down Expand Up @@ -90,7 +94,7 @@ class AsyncEventSourceClient {

//system callbacks (do not call)
void _onAck(size_t len, uint32_t time);
void _onPoll();
void _onPoll();
void _onTimeout(uint32_t time);
void _onDisconnect();
};
Expand Down
Loading