Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Added onDisconnect#173

Open
faceless2 wants to merge 1 commit intome-no-dev:masterfrom
faceless2:master
Open

Added onDisconnect#173
faceless2 wants to merge 1 commit intome-no-dev:masterfrom
faceless2:master

Conversation

@faceless2
Copy link
Copy Markdown

I've added an "onDisconnect" handler to the AsyncCallbackWebHandler class. The reasoning behind this is when uploading content as part of the request body, resources may need to be allocated to handle this. But if the upload terminates early there was no way to free those resources (well, none that I could idenitfy).

With this patch I can now do this:

webserver.on("/write", HTTP_POST, [](AsyncWebServerRequest *request) {
        // handle request
    }, NULL, [](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
        // allocate resources
    }).onDisconnect([](AsyncWebServerRequest *request) {
        // free resources
    });

and know that the "onDisconnect" method will always be called, even on error.

@me-no-dev
Copy link
Copy Markdown
Owner

maybe implement this in the base web handler class?

@me-no-dev
Copy link
Copy Markdown
Owner

Actually wait... you should be allocating those resources in the Response classes and not in the Handler classes. Basically you will get the disconnect while you are responding, request disconnects are handled already.

@luc-github
Copy link
Copy Markdown
Contributor

luc-github commented Nov 14, 2017

@faceless2 @me-no-dev
I also need such feature to know when a request is closed for any reason (succeed or error).
On Async I think such mecanism is mandatory and we need a solution, so here my proposal

Instead of adding a new parameter in request creation, the call is defined in request itself to make it as flexible as possible:

in ESPAsyncWebServer.h

class AsyncWebServerRequest {
    private:
    std::function<void(void)> _onDisconnectfn;
    ....
    public:
    void onDisconnect (std::function<void(void)> fn);
    ....

in WebRequest.cpp

    void AsyncWebServerRequest::onDisconnect (std::function<void(void)> fn){
    _onDisconnectfn=fn;
    }
void AsyncWebServerRequest::_onDisconnect(){
  //os_printf("d\n");
  if(_onDisconnectfn) {
      _onDisconnectfn();
    }
  _server->_handleDisconnect(this);
}

So in any handler that need a disconnect call, the customized function can be added (or not):

request->onDisconnect([](){Serial.println("We are done!");});

What do you think ? I know the style is little bit raw, any chance to get such thing implemented ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants