Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use getWebServerPtr to reuse fileManager WebServer ? #15

Open
g1ra opened this issue Feb 2, 2025 · 2 comments
Open

How to use getWebServerPtr to reuse fileManager WebServer ? #15

g1ra opened this issue Feb 2, 2025 · 2 comments

Comments

@g1ra
Copy link

g1ra commented Feb 2, 2025

I'm trying to use fileManager for other purpose , but I'm getting lost with how to use the pointer . Can you help ?
It have something to do with "lambda expression". .. idk

  WebServer *ws = filemgr.getWebServerPtr();

  ws->on("/f", []() {
    ws->setContentLength(CONTENT_LENGTH_UNKNOWN);
    String res = "";
    res->reserve(CTRLASSIST_STREAM_CHUNKSIZE);
    while (ctrl.getHtmlChunk(res)) {
      ws->sendContent(res);
    }
  });

Error :

src/UV_Timer_v4.cpp:836:5: error: 'ws' is not captured
     ws->setContentLength(CONTENT_LENGTH_UNKNOWN);
     ^~
src/UV_Timer_v4.cpp:835:26: note: the lambda has no capture-default
   ws->on("/d", [WebServer]() {
                          ^
src/UV_Timer_v4.cpp:823:14: note: 'WebServer* ws' declared here
   WebServer *ws = filemgr.getWebServerPtr();
              ^~


@g1ra
Copy link
Author

g1ra commented Feb 2, 2025

fixed by passing ws to lambda fn scope
in my crude understanding , I have to pass ws variable to a lambda function to be available in that scope .
http://www.cprogramming.com/c++11/c++11-lambda-closures.html

correct code is :

  WebServer *ws = filemgr.getWebServerPtr();

  ws->on("/f", [ws]() {
    ws->setContentLength(CONTENT_LENGTH_UNKNOWN);
    String res = "";
    res.reserve(CTRLASSIST_STREAM_CHUNKSIZE);
    while (ctrl.getHtmlChunk(res)) {
      ws->sendContent(res);
    }
  });

Now another problem arise , that the IPADDR/f returns the HTML content in <body><pre>

instead of running like did it before . I'm trying to serve https://github.com/gemi254/ControlAssist-ESP32-ESP8266

@holgerlembke
Copy link
Owner

No sure what I need to help here...

For lambdas, it might become clearer if you do not use a lambda but a traditional approach with a function here. That should show what is needed to pass into the lambda. (that said, early in the morning without enough coffee.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants