Skip to content

Commit c919f61

Browse files
authored
update documentation example (esp8266#7697)
1 parent 5d2563e commit c919f61

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

doc/esp8266wifi/server-examples.rst

+49-26
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ Then let's write a short function ``prepareHtmlPage()``, that will return a ``St
3939
4040
String prepareHtmlPage()
4141
{
42-
String htmlPage =
43-
String("HTTP/1.1 200 OK\r\n") +
44-
"Content-Type: text/html\r\n" +
45-
"Connection: close\r\n" + // the connection will be closed after completion of the response
46-
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
47-
"\r\n" +
48-
"<!DOCTYPE HTML>" +
49-
"<html>" +
50-
"Analog input: " + String(analogRead(A0)) +
51-
"</html>" +
52-
"\r\n";
42+
String htmlPage;
43+
htmlPage.reserve(1024); // prevent ram fragmentation
44+
htmlPage = F("HTTP/1.1 200 OK\r\n"
45+
"Content-Type: text/html\r\n"
46+
"Connection: close\r\n" // the connection will be closed after completion of the response
47+
"Refresh: 5\r\n" // refresh the page automatically every 5 sec
48+
"\r\n"
49+
"<!DOCTYPE HTML>"
50+
"<html>"
51+
"Analog input: ");
52+
htmlPage += analogRead(A0);
53+
htmlPage += F("</html>"
54+
"\r\n");
5355
return htmlPage;
5456
}
5557
@@ -79,7 +81,7 @@ The content contains two basic `HTML <https://www.w3schools.com/html/>`__ tags,
7981

8082
.. code:: cpp
8183
82-
String(analogRead(A0))
84+
analogRead(A0)
8385
8486
The Page is Served
8587
~~~~~~~~~~~~~~~~~~
@@ -90,7 +92,7 @@ Serving of this web page will be done in the ``loop()`` where server is waiting
9092
9193
void loop()
9294
{
93-
WiFiClient client = server.available();
95+
WiFiClient client = server.available();
9496
if (client)
9597
{
9698
// we have a new client sending some request
@@ -126,6 +128,18 @@ The whole process is concluded by stopping the connection with client:
126128
127129
client.stop();
128130
131+
But before that, we must not interrupt client's request:
132+
133+
.. code:: cpp
134+
135+
while (client.available()) {
136+
// but first, let client finish its request
137+
// that's diplomatic compliance to protocols
138+
// (and otherwise some clients may complain, like curl)
139+
// (that is an example, prefer using a proper webserver library)
140+
client.read();
141+
}
142+
129143
Put it Together
130144
~~~~~~~~~~~~~~~
131145

@@ -163,24 +177,26 @@ Complete sketch is presented below.
163177
// prepare a web page to be send to a client (web browser)
164178
String prepareHtmlPage()
165179
{
166-
String htmlPage =
167-
String("HTTP/1.1 200 OK\r\n") +
168-
"Content-Type: text/html\r\n" +
169-
"Connection: close\r\n" + // the connection will be closed after completion of the response
170-
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
171-
"\r\n" +
172-
"<!DOCTYPE HTML>" +
173-
"<html>" +
174-
"Analog input: " + String(analogRead(A0)) +
175-
"</html>" +
176-
"\r\n";
180+
String htmlPage;
181+
htmlPage.reserve(1024); // prevent ram fragmentation
182+
htmlPage = F("HTTP/1.1 200 OK\r\n"
183+
"Content-Type: text/html\r\n"
184+
"Connection: close\r\n" // the connection will be closed after completion of the response
185+
"Refresh: 5\r\n" // refresh the page automatically every 5 sec
186+
"\r\n"
187+
"<!DOCTYPE HTML>"
188+
"<html>"
189+
"Analog input: ");
190+
htmlPage += analogRead(A0);
191+
htmlPage += F("</html>"
192+
"\r\n");
177193
return htmlPage;
178194
}
179195
180196
181197
void loop()
182198
{
183-
WiFiClient client = server.available();
199+
WiFiClient client = server.available();
184200
// wait for a client (web browser) to connect
185201
if (client)
186202
{
@@ -200,7 +216,14 @@ Complete sketch is presented below.
200216
}
201217
}
202218
}
203-
delay(1); // give the web browser time to receive the data
219+
220+
while (client.available()) {
221+
// but first, let client finish its request
222+
// that's diplomatic compliance to protocols
223+
// (and otherwise some clients may complain, like curl)
224+
// (that is an example, prefer using a proper webserver library)
225+
client.read();
226+
}
204227
205228
// close the connection:
206229
client.stop();

0 commit comments

Comments
 (0)