Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By beic
#76546
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
User avatar
By QuickFix
#76547 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);
  }
}
User avatar
By beic
#76549
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?
User avatar
By Cosmic Mac
#76550 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... ;)