You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So im making a project that needs to deal with 2 simultaneous requests so i have created 2 instances of asyncHTTPrequest and i call one per request i need to do. Example code below:
void CountinoWebConfig::publishCounts2(counterData_t counterData){
//retrieve last counter data formatted
String lastCounterData = formatData(counterData.sequenceNum, myTimeTracker.getLocalUnixTimeString(), counterData.periodDurationMs, counterData.countsInThisPeriod,counterData.totalCounts, counterData.minCountsPerSecondInThisPeriod, counterData.maxCountsPerSecondInThisPeriod);
//insert data in buffer if space available
if ( myConfigFromPortal.UploadAPI != (char*)"" ){
if (_requestBuffer_Counter < CWC_REQUESTBUFFERLEN){
_requestBuffer[_requestBuffer_Counter] = lastCounterData;
_requestBuffer_Counter++;
}
//append previous buffered reccords
String allData = "";
for (uint16_t i= 0 ; i<_requestBuffer_Counter; i++){
if (i==0){
allData += _requestBuffer[i];
}else{
allData+=','+_requestBuffer[i];
}
}
String dataToSend = getJSON(allData); //jsonify data
if (_hasConnectivity && ! _isPublishing){
Serial.print("[CountinoWebConfig] Will Send POST\n");
_isPublishing = true;
request.setDebug(true);
//prepare request callback
request.onReadyStateChange([this](void* optParm, asyncHTTPrequest* request, int readyState){
if(readyState == 4){
int httpCode = request->responseHTTPcode();
Serial.printf("[CountinoWebConfig] HTTP response text : %s\n", request->responseText().c_str());
Serial.printf("[CountinoWebConfig] HTTP CODE is: %d\n ", httpCode );
if (httpCode == 200){
signalPortalPushSuccess(true);
_requestBufferCustomUrl_Counter=0;
}else{
signalPortalPushSuccess(false);
}
request->setDebug(false);
_isPublishing = false;
}
});
//send request
if(request.readyState() == 0 || request.readyState() == 4){
request.open("POST", myConfigFromPortal.UploadAPI.c_str());
request.setReqHeader("Connection", "close");
request.setReqHeader("Content-Type", "application/json");
request.setReqHeader("Content-Length", String(dataToSend.length()).c_str());
request.send(dataToSend);
}
}else{
signalPortalPushSuccess(false);
}
}
//Do the same for custom portal
//insert data in buffer if space available
if ( myConfigFromPortal.data_destination_detail != (char*)"" ){
if (_requestBufferCustomUrl_Counter < CWC_REQUESTBUFFERLEN){
_requestBufferCustomUrl[_requestBufferCustomUrl_Counter] = lastCounterData;
_requestBufferCustomUrl_Counter++;
}
//append previous buffered reccords
String allData = "";
for (uint16_t i= 0 ; i<_requestBufferCustomUrl_Counter; i++){
if (i==0){
allData += _requestBufferCustomUrl[i];
}else{
allData+=','+_requestBufferCustomUrl[i];
}
}
String dataToSend = getJSON(allData); //jsonify data
if (_hasConnectivity && !_isPublishingCustom){
Serial.print("[CountinoWebConfig] Will Send POST to Custom URL\n");
_isPublishingCustom = true;
requestCustom.setDebug(true);
requestCustom.onReadyStateChange([this](void* optParm, asyncHTTPrequest* requestCust, int readyState){
if(readyState == 4){
int httpCode = requestCust->responseHTTPcode();
Serial.printf("[CountinoWebConfig] HTTP response text : %s\n", requestCust->responseText().c_str());
Serial.printf("[CountinoWebConfig] HTTP CODE is: %d\n ", httpCode );
if (httpCode == 200){
signalCustomPortalPushSuccess(true);
_requestBufferCustomUrl_Counter=0;
}else{
signalCustomPortalPushSuccess(false);
}
requestCust->setDebug(false);
_isPublishingCustom = false;
}
});
if(requestCustom.readyState() == 0 || requestCustom.readyState() == 4){
requestCustom.open("POST", myConfigFromPortal.data_destination_detail.c_str());
requestCustom.setReqHeader("Connection", "close");
requestCustom.setReqHeader("Content-Type", "application/json");
requestCustom.setReqHeader("Content-Length", String(dataToSend.length()).c_str());
requestCustom.send(dataToSend);
}
}else{
signalCustomPortalPushSuccess(false);
}
}
}
Not sure if im making anything wrong or its the library not handling properly the soimultaneous requests. I did a similar library before but changed to this one because it felt more complete. Help is much appreciated please!
The text was updated successfully, but these errors were encountered:
OK i noticed that if there is no forward slash at the end of the URL, it crashes. I was posting to my local server --> "http://192.168.1.238:8080" and it was missing the ... slash --> 8080/
I think if you checked the boolean return from request.open it would have been false. That said, the subsequent send on the unopened request needs to be bulletproofed, and I'll do so next time I'm working on it. Thanks.
Thanks so much for the quick response. didnt notice that request.open returns boolean. I will protect that but still it was my fault on the forward slash. Good job on this library. Until now, its working very well but if you want to make it perfect --> HTTPS support :)
So im making a project that needs to deal with 2 simultaneous requests so i have created 2 instances of asyncHTTPrequest and i call one per request i need to do. Example code below:
/////////////////////////// OUTPUT /////////////////////
Not sure if im making anything wrong or its the library not handling properly the soimultaneous requests. I did a similar library before but changed to this one because it felt more complete. Help is much appreciated please!
The text was updated successfully, but these errors were encountered: