2016-06-28 11:51:48 -07:00
|
|
|
/*
|
|
|
|
Asynchronous WebServer library for Espressif MCUs
|
|
|
|
|
|
|
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
#ifndef ASYNCEVENTSOURCE_H_
|
|
|
|
#define ASYNCEVENTSOURCE_H_
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
|
|
|
class AsyncEventSource;
|
|
|
|
class AsyncEventSourceResponse;
|
|
|
|
class AsyncEventSourceClient;
|
2016-06-29 09:29:39 -07:00
|
|
|
typedef std::function<void(AsyncEventSourceClient *client)> ArEventHandlerFunction;
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
class AsyncEventSourceClient {
|
|
|
|
private:
|
|
|
|
AsyncClient *_client;
|
|
|
|
AsyncEventSource *_server;
|
2016-06-29 11:21:10 -07:00
|
|
|
uint32_t _lastId;
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AsyncEventSourceClient(AsyncWebServerRequest *request, AsyncEventSource *server);
|
|
|
|
~AsyncEventSourceClient();
|
|
|
|
|
|
|
|
AsyncClient* client(){ return _client; }
|
|
|
|
void close();
|
2016-06-29 09:29:39 -07:00
|
|
|
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);
|
2016-11-27 07:42:09 -08:00
|
|
|
bool connected() const { return (_client != NULL) && _client->connected(); }
|
|
|
|
uint32_t lastId() const { return _lastId; }
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
//system callbacks (do not call)
|
|
|
|
void _onTimeout(uint32_t time);
|
|
|
|
void _onDisconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
class AsyncEventSource: public AsyncWebHandler {
|
|
|
|
private:
|
|
|
|
String _url;
|
2016-11-27 07:42:09 -08:00
|
|
|
LinkedList<AsyncEventSourceClient *> _clients;
|
2016-06-29 09:29:39 -07:00
|
|
|
ArEventHandlerFunction _connectcb;
|
2016-06-28 11:51:48 -07:00
|
|
|
public:
|
2016-11-27 07:42:09 -08:00
|
|
|
AsyncEventSource(const String& url);
|
2016-06-28 11:51:48 -07:00
|
|
|
~AsyncEventSource();
|
|
|
|
|
2016-11-27 07:42:09 -08:00
|
|
|
const char * url() const { return _url.c_str(); }
|
2016-06-28 11:51:48 -07:00
|
|
|
void close();
|
2016-06-29 09:29:39 -07:00
|
|
|
void onConnect(ArEventHandlerFunction cb);
|
2016-06-28 11:51:48 -07:00
|
|
|
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
|
2016-11-27 07:42:09 -08:00
|
|
|
size_t count() const; //number clinets connected
|
2016-06-28 11:51:48 -07:00
|
|
|
|
|
|
|
//system callbacks (do not call)
|
|
|
|
void _addClient(AsyncEventSourceClient * client);
|
|
|
|
void _handleDisconnect(AsyncEventSourceClient * client);
|
2016-11-27 07:42:09 -08:00
|
|
|
virtual bool canHandle(AsyncWebServerRequest *request) override final;
|
|
|
|
virtual void handleRequest(AsyncWebServerRequest *request) override final;
|
2016-06-28 11:51:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class AsyncEventSourceResponse: public AsyncWebServerResponse {
|
|
|
|
private:
|
|
|
|
String _content;
|
|
|
|
AsyncEventSource *_server;
|
|
|
|
public:
|
|
|
|
AsyncEventSourceResponse(AsyncEventSource *server);
|
|
|
|
void _respond(AsyncWebServerRequest *request);
|
|
|
|
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time);
|
|
|
|
bool _sourceValid(){ return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* ASYNCEVENTSOURCE_H_ */
|