Web Server using commands as parameters. Getting duplicated commands

Hi there.
Im writting a program that, alog with many other tasks, listens for connections on port 80, accepts commands in the URL, and depending on the command, replies with different internal values showing the status of the device.

For some reason, I quite often get the last command executed, not the current one. It’s like something got stored somewhere like in a 1 commadn queue, and if I invoke commands A,B,C,D,E I get the result of nothing, A, B,C,D

If I close my firefox tab and open another one, and access the device from that one, if works fine for that first time, so it seems like getting a new tab discards the pending information.

The way I process commands, is I issue them after an "" character, so I scan the incomming string from the web socket, wait for a ""m and then return the following char as the command to be interpreted.

This is the corresponding code for this:

char GetWebCmd () {
  char ret = 0;
  boolean currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '_' )
        ret = client.read();
      if (c == '\n' && currentLineIsBlank)
        return (ret);
      if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
    }
  }
  return (0);
}


void WebDispatch ()  {
  client.flush();
  char com = GetWebCmd();
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println(F("Connection: close"));  // the connection will be closed after completion of the response
  client.println();
  client.println(F("<!DOCTYPE HTML>"));
  client.println (F("<html><head><title>Web Command Service"));
...
  if (cmd=='A') //do cmd A
  if (cmd=='B') //...
...
  client.println (F("
</body></html>"));
  client.println ();
  client.println ();
  delay (500);
  //while (client.connected() && client.available() ) client.read();
  client.stop();
}

void loop ()  {
...
    client = server.available();
    if (client) WebDispatch();
...


}

Evertything works, commands are dispatched and they show their corresponding information, but like I said, commands are like 1 connection behind

I suspect Im missing something to be flushed, zeroed, emptied…Im leaving something pending in the buffer, not sure thou.

I also had “if client.available() client.read()” at the end of the connection, but this didn’t change anything

Thank you!

    if (client.available()) {
      char c = client.read();
      if (c == '_' )
        ret = client.read();

If there is one byte available for reading, and that byte is '_', read another byte. Does THAT seem like a reasonable thing to do?

Do you mean, Im already reading a second byte before I make sure there's a second one available? Yes it sounds reasonable to "WAIT" for it to be available, I was guessing the ethernet queue was all filled at one and most of the "GET...HTTP/1.1" query was being dumped to the buffer together.

But I still don't understand how I get the commands, but always the one that came in the last "get", not in the current one

Thanks

Oh btw, if the issue was with not granting that the cmd was ready to be read after I get the “_”, I don’t see why it would work well when I close the tab and other a new one, this only fails when I click on a second command, and it brings the last one again, without closing/reopening my tab