add collection of LastEventId header

This commit is contained in:
Me No Dev 2016-06-29 21:21:10 +03:00
parent a5674fab69
commit 8399f0bcb9
2 changed files with 19 additions and 10 deletions

View File

@ -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;
}

View File

@ -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();