multiple connections
Posted: Mon Jul 17, 2017 3:36 pm
Greetings,
At first, when I begined with the esp8266, I handled connections with this method :
Where WEB_PAGE, and CONFIG_WEB_PAGE where two different webpages, with their http headers, but recently i discovered that you could also answer clients by using the handle.client method:
In this last method of answering the client, which I think is more correct than the first one, I am able to answers javascripts and forms, and do more things, but now I am trying to make this last method answer multiple clients simultaneously, something that the 1st method could.
So the final questions are:
1st; How can I answer multiple clients simultaneously with the 2nd method?
(The most similar thing I found on the internet said something about arrays, but I didnt understood it, and I think it uses the first method linkhttps://arduino.stackexchange.com/questions/31256/multiple-client-server-over-wifi)
2nd;Which method is more correct?
3rd:How do you handle connections?
At first, when I begined with the esp8266, I handled connections with this method :
Code: Select all
WiFiClient client = server.available();
if (client)
{
//Serial.println("\n[Client connected]");
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
line.toCharArray(string, 100);
//ANSWERS
//OPTION 1
if (has_been_answered == false) {
if (line.indexOf("/config") != -1) {
client.println(CONFIG_WEB_PAGE());
has_been_answered = true;
break;
}
}
//OPTION 2 (this was the option given to a new connection without arguments : /config)
if (has_been_answered == false) {
if (line.length() == 1 && line[0] == '\n' || line.indexOf("/inicio") != -1)
{
client.println(WEB_PAGE());
has_been_answered = true;
break;
}
}
//ANSWERS END
}
}
client.stop();
Serial.println();
Serial.println();
Serial.println();
//Serial.printf("[Client disonnected]\n");
}
}
Where WEB_PAGE, and CONFIG_WEB_PAGE where two different webpages, with their http headers, but recently i discovered that you could also answer clients by using the handle.client method:
Code: Select all
//DIVIDED INTO 2 PARTS
void setup() { //WHERE YOU SET THE ANSWERS
server.on("/",main_webpage);
//THIS 3 ARE FOR ANSWERING JAVASCRIPTS
server.on("/arg1", function1);
server.on("/arg2", function2);
server.on("/arg3", function3);
server.onNotFound(notfoundfunction);
const char * headerkeys[] = {"User-Agent", "Cookie"} ;
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
//ask server to track these headers
server.collectHeaders(headerkeys, headerkeyssize );
server.begin();
}
//ANSWERING FUNCTIONS
void main_webpage() {
//server.sendHeader("Refresh", "1");
server.send(WEB_PAGE().length(), "text/html", WEB_PAGE());
}
void function1() {
server.send(String(var1).length(), "text/plain", String(var1));
}
void function2() {
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1");
server.send(String(var2).length(), "text/plain", String(var2));
}
void function3() {
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1");
server.send(String(var3).length(), "text/plain", String(var3));
}
void notfoundfunction() {
String message = "ERROR 404\n\n";
//message += "URI: ";
//message += server.uri();
//message += "\nMethod: ";
//message += (server.method() == HTTP_GET) ? "GET" : "POST";
//message += "\nArguments: ";
//message += server.args();
// message += "\n";
//for (uint8_t i = 0; i < server.args(); i++) {
// message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
// }
server.send(message.length(), "text/plain", message);
}
In this last method of answering the client, which I think is more correct than the first one, I am able to answers javascripts and forms, and do more things, but now I am trying to make this last method answer multiple clients simultaneously, something that the 1st method could.
So the final questions are:
1st; How can I answer multiple clients simultaneously with the 2nd method?
(The most similar thing I found on the internet said something about arrays, but I didnt understood it, and I think it uses the first method linkhttps://arduino.stackexchange.com/questions/31256/multiple-client-server-over-wifi)
2nd;Which method is more correct?
3rd:How do you handle connections?