2016-04-23 15:11:32 +03:00
|
|
|
/*
|
|
|
|
|
Asynchronous WebServer library for Espressif MCUs
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
|
|
|
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
|
|
|
|
|
|
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 ASYNCWEBSOCKET_H_
|
|
|
|
|
#define ASYNCWEBSOCKET_H_
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
2017-09-07 22:01:58 +03:00
|
|
|
#ifdef ESP32
|
|
|
|
|
#include <AsyncTCP.h>
|
2018-08-18 21:10:39 +02:00
|
|
|
#define WS_MAX_QUEUED_MESSAGES 32
|
2017-09-07 22:01:58 +03:00
|
|
|
#else
|
2016-04-23 15:11:32 +03:00
|
|
|
#include <ESPAsyncTCP.h>
|
2018-08-18 21:10:39 +02:00
|
|
|
#define WS_MAX_QUEUED_MESSAGES 8
|
2017-09-07 22:01:58 +03:00
|
|
|
#endif
|
2016-04-23 15:11:32 +03:00
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
|
2019-09-12 17:56:13 +01:00
|
|
|
#include "AsyncWebSynchronization.h"
|
|
|
|
|
|
2020-12-29 12:56:39 -05:00
|
|
|
#include <list>
|
2020-12-29 18:49:23 -05:00
|
|
|
#include <deque>
|
2020-12-29 12:56:39 -05:00
|
|
|
|
2019-06-23 06:58:25 +02:00
|
|
|
#ifdef ESP8266
|
|
|
|
|
#include <Hash.h>
|
2019-10-02 04:16:43 -07:00
|
|
|
#ifdef CRYPTO_HASH_h // include Hash.h from espressif framework if the first include was from the crypto library
|
|
|
|
|
#include <../src/Hash.h>
|
|
|
|
|
#endif
|
2019-06-23 06:58:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
2019-09-24 20:42:40 +01:00
|
|
|
#ifdef ESP32
|
|
|
|
|
#define DEFAULT_MAX_WS_CLIENTS 8
|
|
|
|
|
#else
|
|
|
|
|
#define DEFAULT_MAX_WS_CLIENTS 4
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
class AsyncWebSocket;
|
|
|
|
|
class AsyncWebSocketResponse;
|
|
|
|
|
class AsyncWebSocketClient;
|
|
|
|
|
class AsyncWebSocketControl;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Message type as defined by enum AwsFrameType.
|
|
|
|
|
* Note: Applications will only see WS_TEXT and WS_BINARY.
|
|
|
|
|
* All other types are handled by the library. */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint8_t message_opcode;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Frame number of a fragmented message. */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint32_t num;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Is this the last frame in a fragmented message ?*/
|
2016-04-23 15:11:32 +03:00
|
|
|
uint8_t final;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Is this frame masked? */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint8_t masked;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Message type as defined by enum AwsFrameType.
|
|
|
|
|
* This value is the same as message_opcode for non-fragmented
|
|
|
|
|
* messages, but may also be WS_CONTINUATION in a fragmented message. */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint8_t opcode;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Length of the current frame.
|
|
|
|
|
* This equals the total length of the message if num == 0 && final == true */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint64_t len;
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Mask key */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint8_t mask[4];
|
2017-11-06 14:58:07 +01:00
|
|
|
/** Offset of the data inside the current frame. */
|
2016-04-23 15:11:32 +03:00
|
|
|
uint64_t index;
|
|
|
|
|
} AwsFrameInfo;
|
|
|
|
|
|
|
|
|
|
typedef enum { WS_DISCONNECTED, WS_CONNECTED, WS_DISCONNECTING } AwsClientStatus;
|
|
|
|
|
typedef enum { WS_CONTINUATION, WS_TEXT, WS_BINARY, WS_DISCONNECT = 0x08, WS_PING, WS_PONG } AwsFrameType;
|
|
|
|
|
typedef enum { WS_MSG_SENDING, WS_MSG_SENT, WS_MSG_ERROR } AwsMessageStatus;
|
|
|
|
|
typedef enum { WS_EVT_CONNECT, WS_EVT_DISCONNECT, WS_EVT_PONG, WS_EVT_ERROR, WS_EVT_DATA } AwsEventType;
|
|
|
|
|
|
2017-03-14 13:55:39 +00:00
|
|
|
class AsyncWebSocketMessageBuffer {
|
|
|
|
|
private:
|
|
|
|
|
uint8_t * _data;
|
|
|
|
|
size_t _len;
|
2020-09-12 14:40:39 +03:00
|
|
|
bool _lock;
|
|
|
|
|
uint32_t _count;
|
2017-03-14 13:55:39 +00:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AsyncWebSocketMessageBuffer();
|
|
|
|
|
AsyncWebSocketMessageBuffer(size_t size);
|
2020-09-12 14:40:39 +03:00
|
|
|
AsyncWebSocketMessageBuffer(uint8_t * data, size_t size);
|
|
|
|
|
AsyncWebSocketMessageBuffer(const AsyncWebSocketMessageBuffer &);
|
|
|
|
|
AsyncWebSocketMessageBuffer(AsyncWebSocketMessageBuffer &&);
|
|
|
|
|
~AsyncWebSocketMessageBuffer();
|
2019-10-14 05:30:06 -03:00
|
|
|
void operator ++(int i) { (void)i; _count++; }
|
|
|
|
|
void operator --(int i) { (void)i; if (_count > 0) { _count--; } ; }
|
2017-03-14 13:55:39 +00:00
|
|
|
bool reserve(size_t size);
|
|
|
|
|
void lock() { _lock = true; }
|
|
|
|
|
void unlock() { _lock = false; }
|
|
|
|
|
uint8_t * get() { return _data; }
|
|
|
|
|
size_t length() { return _len; }
|
|
|
|
|
uint32_t count() { return _count; }
|
2020-09-12 14:40:39 +03:00
|
|
|
bool canDelete() { return (!_count && !_lock); }
|
2017-03-14 13:55:39 +00:00
|
|
|
|
2020-09-12 14:40:39 +03:00
|
|
|
friend AsyncWebSocket;
|
2017-03-14 13:55:39 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
class AsyncWebSocketMessage {
|
|
|
|
|
protected:
|
|
|
|
|
uint8_t _opcode;
|
|
|
|
|
bool _mask;
|
|
|
|
|
AwsMessageStatus _status;
|
|
|
|
|
public:
|
2016-11-27 17:42:09 +02:00
|
|
|
AsyncWebSocketMessage():_opcode(WS_TEXT),_mask(false),_status(WS_MSG_ERROR){}
|
2016-04-23 15:11:32 +03:00
|
|
|
virtual ~AsyncWebSocketMessage(){}
|
2016-11-27 17:42:09 +02:00
|
|
|
virtual void ack(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))){}
|
|
|
|
|
virtual size_t send(AsyncClient *client __attribute__((unused))){ return 0; }
|
2016-04-23 15:11:32 +03:00
|
|
|
virtual bool finished(){ return _status != WS_MSG_SENDING; }
|
2016-11-27 17:42:09 +02:00
|
|
|
virtual bool betweenFrames() const { return false; }
|
2016-04-23 15:11:32 +03:00
|
|
|
};
|
|
|
|
|
|
2017-03-14 13:55:39 +00:00
|
|
|
class AsyncWebSocketBasicMessage: public AsyncWebSocketMessage {
|
|
|
|
|
private:
|
|
|
|
|
size_t _len;
|
|
|
|
|
size_t _sent;
|
|
|
|
|
size_t _ack;
|
|
|
|
|
size_t _acked;
|
2017-09-09 09:04:47 +03:00
|
|
|
uint8_t * _data;
|
2017-03-14 13:55:39 +00:00
|
|
|
public:
|
|
|
|
|
AsyncWebSocketBasicMessage(const char * data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false);
|
|
|
|
|
AsyncWebSocketBasicMessage(uint8_t opcode=WS_TEXT, bool mask=false);
|
|
|
|
|
virtual ~AsyncWebSocketBasicMessage() override;
|
|
|
|
|
virtual bool betweenFrames() const override { return _acked == _ack; }
|
|
|
|
|
virtual void ack(size_t len, uint32_t time) override ;
|
|
|
|
|
virtual size_t send(AsyncClient *client) override ;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AsyncWebSocketMultiMessage: public AsyncWebSocketMessage {
|
|
|
|
|
private:
|
|
|
|
|
uint8_t * _data;
|
|
|
|
|
size_t _len;
|
|
|
|
|
size_t _sent;
|
|
|
|
|
size_t _ack;
|
|
|
|
|
size_t _acked;
|
2020-12-30 18:05:02 -05:00
|
|
|
AsyncWebSocketMessageBuffer *_WSbuffer;
|
2017-03-14 13:55:39 +00:00
|
|
|
public:
|
2020-09-12 14:40:39 +03:00
|
|
|
AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuffer * buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
2017-03-14 13:55:39 +00:00
|
|
|
virtual ~AsyncWebSocketMultiMessage() override;
|
|
|
|
|
virtual bool betweenFrames() const override { return _acked == _ack; }
|
|
|
|
|
virtual void ack(size_t len, uint32_t time) override ;
|
|
|
|
|
virtual size_t send(AsyncClient *client) override ;
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-30 18:05:02 -05:00
|
|
|
class PolymorphMessageContainer
|
|
|
|
|
{
|
|
|
|
|
union {
|
|
|
|
|
AsyncWebSocketBasicMessage basicMessage;
|
|
|
|
|
AsyncWebSocketMultiMessage multiMessage;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class Type : uint8_t { Basic, Multi };
|
|
|
|
|
const Type type;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
PolymorphMessageContainer() = delete;
|
|
|
|
|
PolymorphMessageContainer(const PolymorphMessageContainer &) = delete;
|
|
|
|
|
PolymorphMessageContainer &operator=(const PolymorphMessageContainer &) = delete;
|
|
|
|
|
|
|
|
|
|
PolymorphMessageContainer(const char *data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false) :
|
|
|
|
|
type{Type::Basic}
|
|
|
|
|
{
|
|
|
|
|
new (&basicMessage) AsyncWebSocketBasicMessage{data, len, opcode, mask};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PolymorphMessageContainer(AsyncWebSocketMessageBuffer *buffer, uint8_t opcode=WS_TEXT, bool mask=false) :
|
|
|
|
|
type{Type::Multi}
|
|
|
|
|
{
|
|
|
|
|
new (&multiMessage) AsyncWebSocketMultiMessage{buffer, opcode, mask};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~PolymorphMessageContainer()
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Type::Basic: basicMessage.~AsyncWebSocketBasicMessage(); break;
|
|
|
|
|
case Type::Multi: multiMessage.~AsyncWebSocketMultiMessage(); break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AsyncWebSocketMessage &get()
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Type::Basic: return basicMessage;
|
|
|
|
|
case Type::Multi: return multiMessage;
|
|
|
|
|
}
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AsyncWebSocketMessage &get() const
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Type::Basic: return basicMessage;
|
|
|
|
|
case Type::Multi: return multiMessage;
|
|
|
|
|
}
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
class AsyncWebSocketClient {
|
|
|
|
|
private:
|
|
|
|
|
AsyncClient *_client;
|
|
|
|
|
AsyncWebSocket *_server;
|
|
|
|
|
uint32_t _clientId;
|
|
|
|
|
AwsClientStatus _status;
|
|
|
|
|
|
2020-12-30 18:05:02 -05:00
|
|
|
AsyncWebLock _lock;
|
|
|
|
|
|
2020-12-29 18:49:23 -05:00
|
|
|
std::deque<AsyncWebSocketControl> _controlQueue;
|
2020-12-30 18:05:02 -05:00
|
|
|
std::deque<PolymorphMessageContainer> _messageQueue;
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
uint8_t _pstate;
|
|
|
|
|
AwsFrameInfo _pinfo;
|
|
|
|
|
|
2016-05-28 03:50:38 +03:00
|
|
|
uint32_t _lastMessageTime;
|
|
|
|
|
uint32_t _keepAlivePeriod;
|
|
|
|
|
|
2020-12-29 18:49:23 -05:00
|
|
|
void _queueControl(uint8_t opcode, uint8_t *data=NULL, size_t len=0, bool mask=false);
|
2020-12-30 18:05:02 -05:00
|
|
|
void _queueMessage(const char *data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false);
|
|
|
|
|
void _queueMessage(AsyncWebSocketMessageBuffer *buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
2016-04-23 15:11:32 +03:00
|
|
|
void _runQueue();
|
2020-09-12 14:40:39 +03:00
|
|
|
void _clearQueue();
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
public:
|
2017-10-01 10:13:11 +08:00
|
|
|
void *_tempObject;
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
AsyncWebSocketClient(AsyncWebServerRequest *request, AsyncWebSocket *server);
|
|
|
|
|
~AsyncWebSocketClient();
|
|
|
|
|
|
|
|
|
|
//client id increments for the given server
|
2020-12-29 17:43:35 -05:00
|
|
|
uint32_t id() const { return _clientId; }
|
|
|
|
|
AwsClientStatus status() const { return _status; }
|
|
|
|
|
AsyncClient* client() { return _client; }
|
|
|
|
|
const AsyncClient* client() const { return _client; }
|
2017-11-06 14:55:58 +01:00
|
|
|
AsyncWebSocket *server(){ return _server; }
|
2020-12-29 17:43:35 -05:00
|
|
|
const AsyncWebSocket *server() const { return _server; }
|
2017-11-06 14:55:58 +01:00
|
|
|
AwsFrameInfo const &pinfo() const { return _pinfo; }
|
2016-05-03 04:49:51 +03:00
|
|
|
|
2020-12-29 17:43:35 -05:00
|
|
|
IPAddress remoteIP() const;
|
|
|
|
|
uint16_t remotePort() const;
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
//control frames
|
|
|
|
|
void close(uint16_t code=0, const char * message=NULL);
|
|
|
|
|
void ping(uint8_t *data=NULL, size_t len=0);
|
|
|
|
|
|
2016-05-28 03:50:38 +03:00
|
|
|
//set auto-ping period in seconds. disabled if zero (default)
|
|
|
|
|
void keepAlivePeriod(uint16_t seconds){
|
|
|
|
|
_keepAlivePeriod = seconds * 1000;
|
|
|
|
|
}
|
|
|
|
|
uint16_t keepAlivePeriod(){
|
|
|
|
|
return (uint16_t)(_keepAlivePeriod / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
//data packets
|
2020-12-30 18:05:02 -05:00
|
|
|
void message(const char *data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false) { _queueMessage(data, len, opcode, mask); }
|
|
|
|
|
void message(AsyncWebSocketMessageBuffer *buffer, uint8_t opcode=WS_TEXT, bool mask=false) { _queueMessage(buffer, opcode, mask); }
|
2020-12-29 17:43:35 -05:00
|
|
|
bool queueIsFull() const;
|
2020-12-29 18:49:23 -05:00
|
|
|
size_t queueLen() const;
|
2016-04-23 15:11:32 +03:00
|
|
|
|
2016-07-14 00:01:13 +03:00
|
|
|
size_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
|
2017-09-07 22:01:58 +03:00
|
|
|
#ifndef ESP32
|
2016-07-14 00:01:13 +03:00
|
|
|
size_t printf_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3)));
|
2017-09-07 22:01:58 +03:00
|
|
|
#endif
|
2016-04-23 15:11:32 +03:00
|
|
|
void text(const char * message, size_t len);
|
|
|
|
|
void text(const char * message);
|
|
|
|
|
void text(uint8_t * message, size_t len);
|
|
|
|
|
void text(char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void text(const String &message);
|
2016-05-12 14:17:35 +02:00
|
|
|
void text(const __FlashStringHelper *data);
|
2020-09-12 14:40:39 +03:00
|
|
|
void text(AsyncWebSocketMessageBuffer *buffer);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void binary(const char * message, size_t len);
|
|
|
|
|
void binary(const char * message);
|
|
|
|
|
void binary(uint8_t * message, size_t len);
|
|
|
|
|
void binary(char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void binary(const String &message);
|
2016-05-12 14:17:35 +02:00
|
|
|
void binary(const __FlashStringHelper *data, size_t len);
|
2020-09-12 14:40:39 +03:00
|
|
|
void binary(AsyncWebSocketMessageBuffer *buffer);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
2020-12-30 18:05:02 -05:00
|
|
|
bool canSend() const;
|
2019-06-22 19:47:43 +03:00
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
//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();
|
2018-07-24 11:22:54 -07:00
|
|
|
void _onData(void *pbuf, size_t plen);
|
2016-04-23 15:11:32 +03:00
|
|
|
};
|
|
|
|
|
|
2020-05-09 16:55:57 -04:00
|
|
|
typedef std::function<bool(AsyncWebServerRequest *request)> AwsHandshakeHandler;
|
2016-04-23 15:11:32 +03:00
|
|
|
typedef std::function<void(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)> AwsEventHandler;
|
|
|
|
|
|
|
|
|
|
//WebServer Handler implementation that plays the role of a socket server
|
|
|
|
|
class AsyncWebSocket: public AsyncWebHandler {
|
|
|
|
|
private:
|
|
|
|
|
String _url;
|
2020-12-29 18:10:26 -05:00
|
|
|
std::list<AsyncWebSocketClient> _clients;
|
2016-04-23 15:11:32 +03:00
|
|
|
uint32_t _cNextId;
|
|
|
|
|
AwsEventHandler _eventHandler;
|
2020-05-09 16:55:57 -04:00
|
|
|
AwsHandshakeHandler _handshakeHandler;
|
2016-05-25 19:48:04 +03:00
|
|
|
bool _enabled;
|
2019-09-12 17:56:13 +01:00
|
|
|
AsyncWebLock _lock;
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
public:
|
2016-11-27 17:42:09 +02:00
|
|
|
AsyncWebSocket(const String& url);
|
2016-04-23 15:11:32 +03:00
|
|
|
~AsyncWebSocket();
|
2016-11-27 17:42:09 +02:00
|
|
|
const char * url() const { return _url.c_str(); }
|
2016-05-25 19:48:04 +03:00
|
|
|
void enable(bool e){ _enabled = e; }
|
2016-11-27 17:42:09 +02:00
|
|
|
bool enabled() const { return _enabled; }
|
2019-06-22 18:51:01 +02:00
|
|
|
bool availableForWriteAll();
|
|
|
|
|
bool availableForWrite(uint32_t id);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
2016-11-27 17:42:09 +02:00
|
|
|
size_t count() const;
|
2016-04-23 15:11:32 +03:00
|
|
|
AsyncWebSocketClient * client(uint32_t id);
|
|
|
|
|
bool hasClient(uint32_t id){ return client(id) != NULL; }
|
|
|
|
|
|
|
|
|
|
void close(uint32_t id, uint16_t code=0, const char * message=NULL);
|
|
|
|
|
void closeAll(uint16_t code=0, const char * message=NULL);
|
2019-09-24 20:42:40 +01:00
|
|
|
void cleanupClients(uint16_t maxClients = DEFAULT_MAX_WS_CLIENTS);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void ping(uint32_t id, uint8_t *data=NULL, size_t len=0);
|
2017-03-14 13:55:39 +00:00
|
|
|
void pingAll(uint8_t *data=NULL, size_t len=0); // done
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void text(uint32_t id, const char * message, size_t len);
|
|
|
|
|
void text(uint32_t id, const char * message);
|
|
|
|
|
void text(uint32_t id, uint8_t * message, size_t len);
|
|
|
|
|
void text(uint32_t id, char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void text(uint32_t id, const String &message);
|
2016-05-12 14:17:35 +02:00
|
|
|
void text(uint32_t id, const __FlashStringHelper *message);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void textAll(const char * message, size_t len);
|
|
|
|
|
void textAll(const char * message);
|
|
|
|
|
void textAll(uint8_t * message, size_t len);
|
|
|
|
|
void textAll(char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void textAll(const String &message);
|
2017-03-14 13:55:39 +00:00
|
|
|
void textAll(const __FlashStringHelper *message); // need to convert
|
2020-12-30 18:05:02 -05:00
|
|
|
void textAll(AsyncWebSocketMessageBuffer *buffer);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void binary(uint32_t id, const char * message, size_t len);
|
|
|
|
|
void binary(uint32_t id, const char * message);
|
|
|
|
|
void binary(uint32_t id, uint8_t * message, size_t len);
|
|
|
|
|
void binary(uint32_t id, char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void binary(uint32_t id, const String &message);
|
2016-05-12 14:17:35 +02:00
|
|
|
void binary(uint32_t id, const __FlashStringHelper *message, size_t len);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
void binaryAll(const char * message, size_t len);
|
|
|
|
|
void binaryAll(const char * message);
|
|
|
|
|
void binaryAll(uint8_t * message, size_t len);
|
|
|
|
|
void binaryAll(char * message);
|
2016-07-25 18:07:02 +01:00
|
|
|
void binaryAll(const String &message);
|
2016-05-12 14:17:35 +02:00
|
|
|
void binaryAll(const __FlashStringHelper *message, size_t len);
|
2020-12-30 18:05:02 -05:00
|
|
|
void binaryAll(AsyncWebSocketMessageBuffer *buffer);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
2020-12-30 18:05:02 -05:00
|
|
|
void message(uint32_t id, const char *data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false);
|
|
|
|
|
void message(uint32_t id, AsyncWebSocketMessageBuffer *buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
|
|
|
|
void messageAll(AsyncWebSocketMessageBuffer *buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
2016-04-23 15:11:32 +03:00
|
|
|
|
2016-07-14 00:01:13 +03:00
|
|
|
size_t printf(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
|
|
|
|
|
size_t printfAll(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
|
2017-09-07 22:01:58 +03:00
|
|
|
#ifndef ESP32
|
2016-07-14 00:01:13 +03:00
|
|
|
size_t printf_P(uint32_t id, PGM_P formatP, ...) __attribute__ ((format (printf, 3, 4)));
|
2017-09-07 22:01:58 +03:00
|
|
|
#endif
|
2016-07-14 00:01:13 +03:00
|
|
|
size_t printfAll_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3)));
|
2016-04-23 15:11:32 +03:00
|
|
|
|
|
|
|
|
//event listener
|
|
|
|
|
void onEvent(AwsEventHandler handler){
|
|
|
|
|
_eventHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 16:55:57 -04:00
|
|
|
// Handshake Handler
|
|
|
|
|
void handleHandshake(AwsHandshakeHandler handler){
|
|
|
|
|
_handshakeHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-23 15:11:32 +03:00
|
|
|
//system callbacks (do not call)
|
|
|
|
|
uint32_t _getNextId(){ return _cNextId++; }
|
2020-12-29 17:43:35 -05:00
|
|
|
AsyncWebSocketClient *_newClient(AsyncWebServerRequest *request);
|
2016-04-23 15:11:32 +03:00
|
|
|
void _handleDisconnect(AsyncWebSocketClient * client);
|
|
|
|
|
void _handleEvent(AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
|
2016-11-27 17:42:09 +02:00
|
|
|
virtual bool canHandle(AsyncWebServerRequest *request) override final;
|
|
|
|
|
virtual void handleRequest(AsyncWebServerRequest *request) override final;
|
2017-03-14 13:55:39 +00:00
|
|
|
|
|
|
|
|
|
2020-09-12 14:40:39 +03:00
|
|
|
// messagebuffer functions/objects.
|
|
|
|
|
AsyncWebSocketMessageBuffer * makeBuffer(size_t size = 0);
|
|
|
|
|
AsyncWebSocketMessageBuffer * makeBuffer(uint8_t * data, size_t size);
|
2020-12-29 12:56:39 -05:00
|
|
|
std::list<AsyncWebSocketMessageBuffer> _buffers;
|
2020-09-12 14:40:39 +03:00
|
|
|
void _cleanBuffers();
|
2019-10-02 04:16:43 -07:00
|
|
|
|
2020-12-29 18:10:26 -05:00
|
|
|
const std::list<AsyncWebSocketClient> &getClients() const { return _clients; }
|
2016-04-23 15:11:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//WebServer response to authenticate the socket and detach the tcp client from the web server request
|
|
|
|
|
class AsyncWebSocketResponse: public AsyncWebServerResponse {
|
|
|
|
|
private:
|
|
|
|
|
String _content;
|
|
|
|
|
AsyncWebSocket *_server;
|
|
|
|
|
public:
|
2016-11-27 17:42:09 +02:00
|
|
|
AsyncWebSocketResponse(const String& key, AsyncWebSocket *server);
|
2016-04-23 15:11:32 +03:00
|
|
|
void _respond(AsyncWebServerRequest *request);
|
|
|
|
|
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time);
|
2016-11-28 22:00:33 +02:00
|
|
|
bool _sourceValid() const { return true; }
|
2016-04-23 15:11:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* ASYNCWEBSOCKET_H_ */
|