* Server Sent Events example - issue #7008 Illustrates the use of SSE using ESP8266WebServer * Update ServerSentEvents.ino * Create ServerSentEventsMultiClient.ino * sync * Update ServerSentEvents.ino * Update ServerSentEvents.ino Fix missing variables in printf statments Fix subscriptioncount not decreasing Fix SSEBroadcastState (argument sequence wrong) * Undo the library additions, move to current master * Fix compiler warning * Address review and fix multi-sensor updates Address points of @devyte's code review: * Use IPAddress vs. uint32_t * Refactor the URL parsing logic to use strlen vs. sizeof, since there was some confusion in the original (correct) version * Minimize copies of WiFiClients while in use * Use byref access for sensor updates Fix multi-sensor updates * Create an update Ticker for each sensor, because the original code only had one whose callback was overridden by sensorB, meaning sensorA never changed * Fix IPv6 build errors * Remove WiFiClient extraneous copy Avoid duplicating WiFiClient by using the WiFiClient object embedded in the subscriber[] array instead. Co-authored-by: Earle F. Philhower, III <earlephilhower@yahoo.com>
ESP8266 Web Server
The WebServer class found in ESP8266WebServer.h
header,
is a simple web server that knows how to handle HTTP requests such as
GET and POST and can only support one simultaneous client.
Usage
Class Constructor
(80); ESP8266WebServer server
Creates the ESP8266WebServer class object.
Parameters:
host IP address: IPaddress addr
(optional)
host port number: int port
(default is the standard HTTP
port 80)
Basic Operations
Starting the server
void begin();
Handling incoming client requests
void handleClient();
Disabling the server
void close();
void stop();
Both methods function the same
Client request handlers
void on();
void addHandler();
void onNotFound();
void onFileUpload();
Example:
.on("/", handlerFunction);
server.onNotFound(handlerFunction); // called when handler is not assigned
server.onFileUpload(handlerFunction); // handle file uploads server
Sending responses to the client
void send();
void send_P();
Parameters:
code
- HTTP response code, can be 200
or
404
, etc.
content_type
- HTTP content type, like
"text/plain"
or "image/png"
, etc.
content
- actual content body
Advanced Options
Getting information about request arguments
const String & arg();
const String & argName();
int args();
bool hasArg();
arg
- get request argument value, use
arg("plain")
to get POST body
argName
- get request argument name
args
- get arguments count
hasArg
- check if argument exist
Getting information about request headers
const String & header();
const String & headerName();
const String & hostHeader();
int headers();
bool hasHeader();
header
- get request header value
headerName
- get request header name
hostHeader
- get request host header if available, else
empty string
headers
- get header count
hasHeader
- check if header exist
Authentication
bool authenticate();
void requestAuthentication();
authenticate
- server authentication, returns true if
client is authenticated else false
requestAuthentication
- sends authentication failure
response to the client
Example Usage:
if(!server.authenticate(username, password)){
.requestAuthentication();
server}
Other Function Calls
const String & uri(); // get the current uri
(); // get the current method
HTTPMethod method(); // get the current client
WiFiClient client& upload(); // get the current upload
HTTPUpload void setContentLength(); // set content length
void sendHeader(); // send HTTP header
void sendContent(); // send content
void sendContent_P();
void collectHeaders(); // set the request headers to collect
void serveStatic();
size_t streamFile();
For code samples enter here .