Hello,
I'm having a MKR1000 and would like to implement some protocol like this:
Step 1.1: Client -- Req 1 --> Server (MKR1000)
Step 1.2: <-- Rep 1 -- (save some information)
Step 2.1: -- Req 2 -->
Step 2.2: <-- Rep 2 --
where the 2nd pair of request/response is dependent on the 1st one. In particular, I'd like to save some parameters from the first request so that I could use them to process the next one. Currently I'm using codes from the WifiWebServer example here.
For convenience, though the code below does not exactly reflect my protocol, it can demonstrate my problem: a number (n) is initialized when a client is connected and keeps being incremented at the server. For example, if n is initialized to 123 then at step 1.2 and 2.2 the server should send out 124, 125, respectively. However, my problem is the server closes the connection right after finishing the first request so n is reset back to 123, i.e. my client always receives two of 124 instead of 124, 125.
Is there any way that I can keep my session alive? Any advice is much appreciated. Thank you!
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
int n = 123; // INITIALIZE MY NUMBER
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
// EXTRACT INFO FROM CLIENT'S MESSAGE
n = receive_from_client();
if (c == '\n' && currentLineIsBlank) {
n++; // INCREMENT THE NUMBER
// SEND OUT THE NUMBER
send_to_client(n);
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}