-->
Page 2 of 3

Re: Pass data to server.on

PostPosted: Thu Jun 21, 2018 5:24 am
by beic
btidey wrote:If you want two different on calls to do different things then you need to define them as two different methods, e.g. handleRoot and handleRootExt.

If they behave similarly such that you want to make much of the code common then define the common code as say handleRootBase with a parameter to modify its behaviour. Then call handleRootBase from handleRoot and handleRootExt with that parameter.


Thank you for your feedback, and yes, that would be my simplest think to do, but the HTML code is way to big to make a copy of it and to edit/update something in it after would be really hard.

The code / source is the same except at the end, the send command is different as I posted above:

Code: Select allif (blIsExtAccess == false)
  {
    server.send(200, "text/html", stData);
  } else {
    serverExt.send(200, "text/html", stData);
  }
}


So, as you see, I'm sending the same data "stData" to both server command, but with boolean "blIsExtAccess" switch...

That was my intention.

Kind regards,
Viktor

Re: Pass data to server.on

PostPosted: Thu Jun 21, 2018 6:31 am
by QuickFix
A solution, as btidey also suggested, would be something like this:

Code: Select allserver.on("/", handleRoot);
serverExt.on("/", handleRootExt);

void handleRoot;
{
  handleActual(false);
}

void handleRootExt;
{
  handleActual(true);
}

void handleActual(boolean blIsExtAccess)
{
  String stData = "Hello there";

  if (blIsExtAccess == false)
  {
    server.send(200, "text/html", stData);
  } else {
    serverExt.send(200, "text/html", stData);
  }
}

Re: Pass data to server.on

PostPosted: Thu Jun 21, 2018 6:46 am
by beic
QuickFix wrote:A solution, as btidey also suggested, would be something like this:

Code: Select allserver.on("/", handleRoot);
serverExt.on("/", handleRootExt);

void handleRoot;
{
  handleActual(false);
}

void handleRootExt;
{
  handleActual(true);
}

void handleActual(boolean blIsExtAccess)
{
  String stData = "Hello there";

  if (blIsExtAccess == false)
  {
    server.send(200, "text/html", stData);
  } else {
    serverExt.send(200, "text/html", stData);
  }
}


Thank you for your quick answer, but I'm getting error "variable or field 'handleRootL' declared void" and "'handleRootL' was not declared in this scope"

Code: Select allvoid handleRootL;
  {
    handleRoot(false);
  }

  void handleRootE;
  {
    handleRoot(true);
  }
 
  server.on("/", handleRootL);
  serverExt.on("/", handleRootE);


Any thoughts?

Re: Pass data to server.on

PostPosted: Thu Jun 21, 2018 6:52 am
by Cosmic Mac
And you can take advantage of C++ default parameters to simplify the code:
Code: Select allserver.on("/", handleRoot);
serverExt.on("/", handleRootExt);

void handleRootExt() {
   handleRoot(true)
}

void handleRoot(boolean blIsExtAccess = false) {
   String stData = "Hello there";
   if (blIsExtAccess == false) {
      server.send(200, "text/html", stData);
   } else {
      serverExt.send(200, "text/html", stData);
   }
}


Another way would be to redirect Ext requests to the main server:
Code: Select allserverExt.on("/", []() {
   serverExt.sendHeader("Location", String("http://") + toStringIp(serverExt.client().localIP()), true);
   serverExt.send(302, "text/plain", "");
   serverExt.client().stop();
});

server.on("/", []() {
   String stData = "Hello there";
   server.send(200, "text/html", stData);
});


Untested... ;)