arduinoWebSockets
arduinoWebSockets copied to clipboard
Cannot use socket.io and HttpClient at the same time
I'm having a problem with HttpClient when socketio is opening a connection. Httpclient always return fasle(conection failed) when Socketio is connecting. When I disable socketio.loop() or wait for socketion to disconnect due to timeout then I can use httpclient.
Info
- PlatformIO
- links2004/WebSockets@^2.3.6
- bblanchon/ArduinoJson@^6.18.5
- nodemcuv2(esp8266)
API.h like
#ifndef HTTPS_API_HELPER
#define HTTPS_API_HELPER
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#define SSL
class HttpsAPIHelper
{
public:
HttpsAPIHelper(String url);
~HttpsAPIHelper();
bool makeEndInfo(String &json);
private:
// Fingerprints sha-1
const uint8_t fingerprint[20] = {hiden};
String url;
String token;
String url_log_activities;
#ifdef SSL
BearSSL::WiFiClientSecure *client;
#else
WiFiClient *client;
#endif
HTTPClient *https;
StaticJsonDocument<400> postBody;
};
#endif
API.cpp
#include "HttpsAPIHelper.h"
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
HttpsAPIHelper::HttpsAPIHelper(String url)
{
this->url = url;
https = new HTTPClient();
url_log_activities = url + "/sensor-activity-logs";
#ifdef SSL
client = new BearSSL::WiFiClientSecure();
client->setFingerprint(fingerprint);
client->setInsecure();
#else
client = new WiFiClient();
#endif
}
HttpsAPIHelper::~HttpsAPIHelper()
{
delete https;
}
bool HttpsAPIHelper::makeEndInfo(String &json)
{
// https.connected
// https.useHTTP10(true);
if (https->begin(*client, url_log_activities))
{
https->addHeader(F("Content-Type"), F("application/json"));
https->addHeader(F("Accept"), F("application/json"));
system_print_meminfo();
Serial.print("Free heap_size ") ; Serial.println(system_get_free_heap_size());
int httpCode = https->POST(json);
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
// String payload = https->getString();
// Serial.println(payload);
return true;
}
}
else
{
Serial.printf("[HTTPS] POST... failed, error: %s\n", https->errorToString(httpCode).c_str());
Serial.println(url);
}
// https->clear();
https->end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
return false;
}
In main loop() function like this
loop(){
socketio.loop();
if(milis() - timeout >50000)
{
String json = "{\"data1\":55}";
api->makeEndInfo(json);
timeout = milis();
}
}
This is some debugging information
[WS][0][sendFrame] sending Frame Done (133867us).
[wsIOc] send ping
[WS][0][sendFrame] ------- send message frame -------
[WS][0][sendFrame] fin: 1 opCode: 1 mask: 1 length: 1 headerToPayload: 1
[WS][0][sendFrame] text: 2
[write] n: 7 t: 44267
[WS][0][sendFrame] sending Frame Done (36100us).
[WS][0][handleWebsocketWaitFor] size: 2 cWsRXsize: 0
[readCb] n: 2 t: 44412
[WS][0][handleWebsocketWaitFor][readCb] size: 2 ok: 1
[WS][0][handleWebsocket] ------- read massage frame -------
[WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 1
[WS][0][handleWebsocket] mask: 0 payloadLen: 1
[readCb] n: 1 t: 44674
[WS][0][handleWebsocket] text: 3
[wsIOc] get pong
Send to server {"tets":2,"tets_1":"22"}
BSSL:_connectSSL: start connection
BSSL:_connectSSL: OOM error
Has everyone been like this?
I encountered the same problem with beginSSL.
The interesting thing is that esp socketIO can connect to my HTTPS server without fingerprint or CA with the beginSSL() function.
My server does not accept any insecure connection and upgrades every requests to HTTPS. And the socket connection works regardless.
But every HTTP requests ( with CA ) stopped working...
I found the problem. esp8266's memory is too low to run socketio and https in parallel. if you turn off either one it will work fine.
the axTLS can only handle one active TLS / SSL connection. https://github.com/Links2004/arduinoWebSockets/issues/756#issuecomment-1163121901