23#include <ESPAsyncWebServer.h>
26#include <ESP8266mDNS.h>
34#define DOMAIN_NAME "*"
35#define MDNS_HOSTNAME "uniot"
101 return _server.status();
138 mpDnsServer(new DNSServer()),
140 mpWebSocket(new AsyncWebSocket(
WS_URL)),
141 mWebSocketHandler(wsHandler),
142 mWsClientLastSeen(0) {
143 mpWebServer->addHandler(mpWebSocket.get());
161 mpDnsServer->setTTL(30);
162 mpDnsServer->setErrorReplyCode(DNSReplyCode::ServerFailure);
164 if (mWebSocketHandler) {
165 mpWebSocket->enable(
true);
166 mpWebSocket->onEvent([
this](AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type,
void* arg, uint8_t* data,
size_t len) {
167 mWebSocketHandler(server, client, type, arg, data, len);
168 mWsClientLastSeen = millis();
171 mpWebServer->begin();
172 auto status = mpWebServer->status();
179 MDNS.addService(
"http",
"tcp",
HTTP_PORT);
183 return mIsStarted =
true;
205 mMdnsStarted =
false;
210 mpDnsServer.reset(
new DNSServer());
213 mpWebSocket->closeAll();
214 mpWebSocket->cleanupClients();
227 return mpWebServer.get();
239 mpWebSocket->textAll(message);
251 void wsText(uint32_t clientId,
const String& message) {
253 mpWebSocket->text(clientId, message);
265 mpWebSocket->enable(
false);
266 mpWebSocket->closeAll();
267 mpWebSocket->cleanupClients();
281 mpWebSocket->enable(enable);
295 auto timeSinceLastSeen = millis() - mWsClientLastSeen;
296 return (mpWebSocket && mpWebSocket->count() > 0 && timeSinceLastSeen < window);
303 inline const IPAddress&
ip()
const {
321 mpDnsServer->processNextRequest();
324 mpWebSocket->cleanupClients();
342 AwsEventHandler mWebSocketHandler;
343 unsigned long mWsClientLastSeen;
#define HTTP_PORT
Standard HTTP port for web server.
Definition ConfigCaptivePortal.h:32
#define WS_URL
WebSocket endpoint URL path.
Definition ConfigCaptivePortal.h:33
#define MDNS_HOSTNAME
mDNS hostname for local discovery
Definition ConfigCaptivePortal.h:35
#define DOMAIN_NAME
Wildcard domain name for DNS capture.
Definition ConfigCaptivePortal.h:34
#define DNS_PORT
Standard DNS port for captive portal functionality.
Definition ConfigCaptivePortal.h:31
void wsCloseAll()
Close all WebSocket connections.
Definition ConfigCaptivePortal.h:263
virtual void execute(short _) override
Execute periodic captive portal maintenance tasks.
Definition ConfigCaptivePortal.h:318
bool wsClientsActive(unsigned long window=30000) const
Check if WebSocket clients are actively connected.
Definition ConfigCaptivePortal.h:294
void wsText(uint32_t clientId, const String &message)
Send text message to a specific WebSocket client.
Definition ConfigCaptivePortal.h:251
void wsEnable(bool enable)
Enable or disable WebSocket functionality.
Definition ConfigCaptivePortal.h:279
bool start()
Start the captive portal services.
Definition ConfigCaptivePortal.h:159
ConfigCaptivePortal(const IPAddress &apIp, AwsEventHandler wsHandler=nullptr)
Construct a new ConfigCaptivePortal.
Definition ConfigCaptivePortal.h:134
void stop()
Stop all captive portal services.
Definition ConfigCaptivePortal.h:201
const IPAddress & ip() const
Get the IP address of the captive portal.
Definition ConfigCaptivePortal.h:303
void wsTextAll(const String &message)
Send text message to all connected WebSocket clients.
Definition ConfigCaptivePortal.h:237
AsyncWebServer * get()
Get the underlying AsyncWebServer instance.
Definition ConfigCaptivePortal.h:226
Extended AsyncWebServer with status monitoring capabilities.
Definition ConfigCaptivePortal.h:88
uint8_t status()
Get the current server status.
Definition ConfigCaptivePortal.h:100
DetailedAsyncWebServer(uint16_t port)
Construct a new DetailedAsyncWebServer.
Definition ConfigCaptivePortal.h:94
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:152
Contains all classes and functions related to the Uniot Core.