From 8399f0bcb94e33d1d039935b1fd820a20b4dcb9b Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Wed, 29 Jun 2016 21:21:10 +0300 Subject: [PATCH] add collection of LastEventId header --- src/AsyncEventSource.cpp | 23 +++++++++++++++++------ src/AsyncEventSource.h | 6 ++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/AsyncEventSource.cpp b/src/AsyncEventSource.cpp index 4118f7f..a5379ba 100644 --- a/src/AsyncEventSource.cpp +++ b/src/AsyncEventSource.cpp @@ -109,10 +109,11 @@ static String generateEventMessage(const char *message, const char *event, uint3 AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server){ _client = request->client(); _server = server; + _lastId = 0; next = NULL; - //_client->onError([](void *r, AsyncClient* c, int8_t error){ ((AsyncEventSourceClient*)(r))->_onError(error); }, this); - //_client->onAck([](void *r, AsyncClient* c, size_t len, uint32_t time){ ((AsyncEventSourceClient*)(r))->_onAck(len, time); }, this); - //_client->onPoll([](void *r, AsyncClient* c){ ((AsyncEventSourceClient*)(r))->_onPoll(); }, this); + if(request->hasHeader("Last-Event-ID")) + _lastId = atoi(request->getHeader("Last-Event-ID")->value().c_str()); + _client->onError(NULL, NULL); _client->onAck(NULL, NULL); _client->onPoll(NULL, NULL); @@ -127,9 +128,6 @@ AsyncEventSourceClient::~AsyncEventSourceClient(){ close(); } -//void AsyncEventSourceClient::_onAck(size_t len, uint32_t time){} -//void AsyncEventSourceClient::_onPoll(){} -//void AsyncEventSourceClient::_onError(int8_t){} void AsyncEventSourceClient::_onTimeout(uint32_t time){ _client->close(true); } @@ -180,6 +178,18 @@ void AsyncEventSource::onConnect(ArEventHandlerFunction cb){ } void AsyncEventSource::_addClient(AsyncEventSourceClient * client){ + /*char * temp = (char *)malloc(2054); + if(temp != NULL){ + memset(temp+1,' ',2048); + temp[0] = ':'; + temp[2049] = '\r'; + temp[2050] = '\n'; + temp[2051] = '\r'; + temp[2052] = '\n'; + temp[2053] = 0; + client->write((const char *)temp, 2053); + free(temp); + }*/ if(_clients == NULL){ _clients = client; if(_connectcb) @@ -236,6 +246,7 @@ void AsyncEventSource::send(const char *message, const char *event, uint32_t id, bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){ if(request->method() != HTTP_GET || !request->url().equals(_url)) return false; + request->addInterestingHeader("Last-Event-ID"); return true; } diff --git a/src/AsyncEventSource.h b/src/AsyncEventSource.h index 1464f31..2dd30ea 100644 --- a/src/AsyncEventSource.h +++ b/src/AsyncEventSource.h @@ -33,6 +33,7 @@ class AsyncEventSourceClient { private: AsyncClient *_client; AsyncEventSource *_server; + uint32_t _lastId; public: AsyncEventSourceClient * next; @@ -45,11 +46,9 @@ class AsyncEventSourceClient { void write(const char * message, size_t len); void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0); bool connected(){ return (_client != NULL) && _client->connected(); } + uint32_t lastId(){ return _lastId; } //system callbacks (do not call) - //void _onAck(size_t len, uint32_t time); - //void _onError(int8_t); - //void _onPoll(); void _onTimeout(uint32_t time); void _onDisconnect(); }; @@ -59,7 +58,6 @@ class AsyncEventSource: public AsyncWebHandler { String _url; AsyncEventSourceClient * _clients; ArEventHandlerFunction _connectcb; - uint32_t _cNextId; public: AsyncEventSource(String url); ~AsyncEventSource();