@@ -147,11 +147,8 @@ size_t AsyncEventSourceMessage::send(AsyncClient *client) {
147147
148148// Client
149149
150- AsyncEventSourceClient::AsyncEventSourceClient (AsyncWebServerRequest *request, AsyncEventSource *server) : _client(request->clientRelease ()), _server(server) {
151-
152- if (request->hasHeader (T_Last_Event_ID)) {
153- _lastId = atoi (request->getHeader (T_Last_Event_ID)->value ().c_str ());
154- }
150+ AsyncEventSourceClient::AsyncEventSourceClient (AsyncClient *client, AsyncEventSource *server, uint32_t lastId)
151+ : _client(client), _server(server), _lastId(lastId) {
155152
156153 _client->setRxTimeout (0 );
157154 _client->onError (NULL , NULL );
@@ -186,8 +183,6 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
186183
187184 _server->_addClient (this );
188185 _client->setNoDelay (true );
189- // delete AsyncWebServerRequest object (and bound response) since we have the ownership on client connection now
190- delete request;
191186}
192187
193188AsyncEventSourceClient::~AsyncEventSourceClient () {
@@ -478,24 +473,12 @@ AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncEventSource *server) : _
478473void AsyncEventSourceResponse::_respond (AsyncWebServerRequest *request) {
479474 String out;
480475 _assembleHead (out, request->version ());
481- // unbind client's onAck callback from AsyncWebServerRequest's, we will destroy it on next callback and steal the client,
482- // can't do it now 'cause now we are in AsyncWebServerRequest::_onAck 's stack actually
483- // here we are loosing time on one RTT delay, but with current design we can't get rid of Req/Resp objects other way
484- _request = request;
485- request->client ()->onAck (
486- [](void *r, AsyncClient *c, size_t len, uint32_t time) {
487- if (len) {
488- static_cast <AsyncEventSourceResponse *>(r)->_switchClient ();
489- }
490- },
491- this
492- );
476+ uint32_t lastId = 0 ;
477+ if (request->hasHeader (T_Last_Event_ID)) {
478+ lastId = strtoul (request->getHeader (T_Last_Event_ID)->value ().c_str (), nullptr , 10 );
479+ }
493480 request->client ()->write (out.c_str (), _headLength);
494- _state = RESPONSE_WAIT_ACK ;
481+ // Add a new AsyncEventSourceClient to the server's list of clients
482+ // This adopts the ownership of the AsyncTCP's client pointer from `request` parameter
483+ new AsyncEventSourceClient (request->clientRelease (), _server, lastId);
495484}
496-
497- void AsyncEventSourceResponse::_switchClient () {
498- // AsyncEventSourceClient c-tor will take the ownership of AsyncTCP's client connection
499- new AsyncEventSourceClient (_request, _server);
500- // AsyncEventSourceClient c-tor would also delete _request and *this
501- };
0 commit comments