From 1d61453563f62430d441630e642737bb8aad5406 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Mon, 6 Jan 2025 01:13:09 +0100 Subject: [PATCH] Revert to esp websocket client because websocketsClient does not work --- src/lib/block_notify.cpp | 195 ++++++++++++++++++++++----------------- src/lib/block_notify.hpp | 11 +-- src/lib/v2_notify.cpp | 2 +- 3 files changed, 114 insertions(+), 94 deletions(-) diff --git a/src/lib/block_notify.cpp b/src/lib/block_notify.cpp index 4c32a2a..8502e3d 100644 --- a/src/lib/block_notify.cpp +++ b/src/lib/block_notify.cpp @@ -1,103 +1,116 @@ #include "block_notify.hpp" // Initialize static members -WebSocketsClient BlockNotify::webSocket; +esp_websocket_client_handle_t BlockNotify::wsClient = nullptr; uint32_t BlockNotify::currentBlockHeight = 878000; uint16_t BlockNotify::blockMedianFee = 1; bool BlockNotify::notifyInit = false; unsigned long int BlockNotify::lastBlockUpdate = 0; TaskHandle_t BlockNotify::taskHandle = nullptr; -void BlockNotify::taskNotify(void* pvParameters) -{ - for (;;) - { - webSocket.loop(); - vTaskDelay(10 / portTICK_PERIOD_MS); - } -} +const char* BlockNotify::mempoolWsCert = R"EOF( +-----BEGIN CERTIFICATE----- +MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB +iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl +cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV +BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw +MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV +BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU +aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAs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7gtNn3/3 +ATiUFJVDBwn7YKnuHKsSjKCaXqeYalltiz8I+8jRRa8YFWSQEg9zKC7F4iRO/Fjs +8PRF/iKz6y+O0tlFYQXBl2+odnKPi4w2r78NBc5xjeambx9spnFixdjQg3IM8WcR +iQycE0xyNN+81XHfqnHd4blsjDwSXWXavVcStkNr/+XeTWYRUc+ZruwXtuhxkYze +Sf7dNXGiFSeUHM9h4ya7b6NnJSFd5t0dCy5oGzuCr+yDZ4XUmFF0sbmZgIn/f3gZ +XHlKYC6SQK5MNyosycdiyA5d9zZbyuAlJQG03RoHnHcAP9Dc1ew91Pq7P8yF1m9/ +qS3fuQL39ZeatTXaw2ewh0qpKJ4jjv9cJ2vhsE/zB+4ALtRZh8tSQZXq9EfX7mRB +VXyNWQKV3WKdwrnuWih0hKWbt5DHDAff9Yk2dDLWKMGwsAvgnEzDHNb842m1R0aB +L6KCq9NjRHDEjf8tM7qtj3u1cIiuPhnPQCjY/MiQu12ZIvVS5ljFH4gxQ+6IHdfG +jjxDah2nGN59PRbxYvnKkKj9 +-----END CERTIFICATE----- +)EOF"; -void BlockNotify::setupTask() -{ - xTaskCreate(taskNotify, "blockNotify", (6 * 1024), NULL, tskIDLE_PRIORITY, - &taskHandle); -} +void BlockNotify::onWebsocketEvent(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data; + BlockNotify& instance = BlockNotify::getInstance(); -void BlockNotify::onWebsocketEvent(WStype_t type, uint8_t* payload, size_t length) { - switch(type) { - case WStype_DISCONNECTED: { - Serial.println(F("Mempool.space WS Connection Closed")); - break; - } - case WStype_CONNECTED: { + switch (event_id) { + case WEBSOCKET_EVENT_CONNECTED: + { notifyInit = true; Serial.print(F("Connected to ")); Serial.println(preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE)); JsonDocument doc; doc["action"] = "want"; - JsonArray data = doc.createNestedArray("data"); - data.add("blocks"); - data.add("mempool-blocks"); + JsonArray dataArray = doc.createNestedArray("data"); + dataArray.add("blocks"); + dataArray.add("mempool-blocks"); String sub; serializeJson(doc, sub); - Serial.println(sub); - webSocket.sendTXT(sub.c_str()); + esp_websocket_client_send_text(wsClient, sub.c_str(), sub.length(), portMAX_DELAY); break; } - case WStype_TEXT: { - JsonDocument doc; - JsonDocument filter; - filter["block"]["height"] = true; - filter["mempool-blocks"][0]["medianFee"] = true; - - deserializeJson(doc, (char*)payload, DeserializationOption::Filter(filter)); - - if (debugLogEnabled()) { - Serial.println(doc.as()); - } - - if (doc["block"].is()) - { - JsonObject block = doc["block"]; - if (block["height"].as() != currentBlockHeight) { - BlockNotify::getInstance().processNewBlock(block["height"].as()); - } - } - else if (doc["mempool-blocks"].is()) - { - JsonArray blockInfo = doc["mempool-blocks"].as(); - uint medianFee = (uint)round(blockInfo[0]["medianFee"].as()); - BlockNotify::getInstance().processNewBlockFee(medianFee); - } + case WEBSOCKET_EVENT_DATA: + instance.onWebsocketMessage(data); break; - } - case WStype_BIN: - case WStype_ERROR: - case WStype_FRAGMENT_TEXT_START: - case WStype_FRAGMENT_BIN_START: - case WStype_FRAGMENT: - case WStype_PING: - case WStype_PONG: - case WStype_FRAGMENT_FIN: { + + case WEBSOCKET_EVENT_DISCONNECTED: + Serial.println(F("Mempool.space WS Connection Closed")); + break; + + case WEBSOCKET_EVENT_ERROR: + Serial.println(F("Mempool.space WS Connection Error")); break; - } } } -void BlockNotify::setup() -{ +void BlockNotify::onWebsocketMessage(esp_websocket_event_data_t *data) { + JsonDocument doc; + JsonDocument filter; + filter["block"]["height"] = true; + filter["mempool-blocks"][0]["medianFee"] = true; + + deserializeJson(doc, (char*)data->data_ptr, DeserializationOption::Filter(filter)); + + if (doc["block"].is()) { + JsonObject block = doc["block"]; + if (block["height"].as() != currentBlockHeight) { + processNewBlock(block["height"].as()); + } + } + else if (doc["mempool-blocks"].is()) { + JsonArray blockInfo = doc["mempool-blocks"].as(); + uint medianFee = (uint)round(blockInfo[0]["medianFee"].as()); + processNewBlockFee(medianFee); + } +} + +void BlockNotify::setup() { IPAddress result; int dnsErr = -1; String mempoolInstance = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE); - while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':')) - { + while (dnsErr != 1 && !strchr(mempoolInstance.c_str(), ':')) { dnsErr = WiFi.hostByName(mempoolInstance.c_str(), result); - if (dnsErr != 1) - { + if (dnsErr != 1) { Serial.print(mempoolInstance); Serial.println(F("mempool DNS could not be resolved")); WiFi.reconnect(); @@ -111,35 +124,39 @@ void BlockNotify::setup() if (blockFetch > currentBlockHeight) currentBlockHeight = blockFetch; - if (currentBlockHeight != -1) - { + if (currentBlockHeight != -1) { lastBlockUpdate = esp_timer_get_time() / 1000000; } - if (workQueue != nullptr) - { + if (workQueue != nullptr) { WorkItem blockUpdate = {TASK_BLOCK_UPDATE, 0}; xQueueSend(workQueue, &blockUpdate, portMAX_DELAY); } const bool useSSL = preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE); - const int port = useSSL ? 443 : 80; + const String protocol = useSSL ? "wss" : "ws"; + String wsUri = protocol + "://" + mempoolInstance + "/api/v1/ws"; + + esp_websocket_client_config_t config = { + .task_stack = (6*1024), + .user_agent = USER_AGENT + }; if (useSSL) { - webSocket.beginSSL(mempoolInstance.c_str(), port, "/api/v1/ws"); -// webSocket.beginSSL("ws.btclock.dev", port, "/api/v1/ws"); - - } else { - webSocket.begin(mempoolInstance.c_str(), port, "/api/v1/ws"); + config.cert_pem = mempoolWsCert; } - webSocket.onEvent(onWebsocketEvent); - webSocket.setReconnectInterval(5000); - webSocket.enableHeartbeat(15000, 3000, 2); + config.uri = wsUri.c_str(); - setupTask(); + Serial.printf("Connecting to %s\r\n", mempoolInstance.c_str()); + + wsClient = esp_websocket_client_init(&config); + esp_websocket_register_events(wsClient, WEBSOCKET_EVENT_ANY, onWebsocketEvent, wsClient); + esp_websocket_client_start(wsClient); } + + void BlockNotify::processNewBlock(uint32_t newBlockHeight) { if (newBlockHeight <= currentBlockHeight) { @@ -215,7 +232,9 @@ void BlockNotify::setBlockMedianFee(uint16_t newBlockMedianFee) bool BlockNotify::isConnected() const { - return webSocket.isConnected(); + if (wsClient == NULL) + return false; + return esp_websocket_client_is_connected(wsClient); } bool BlockNotify::isInitialized() const @@ -225,11 +244,13 @@ bool BlockNotify::isInitialized() const void BlockNotify::stop() { - webSocket.disconnect(); - if (taskHandle != NULL) { - vTaskDelete(taskHandle); - taskHandle = NULL; - } + if (wsClient == NULL) + return; + + esp_websocket_client_close(wsClient, portMAX_DELAY); + esp_websocket_client_stop(wsClient); + esp_websocket_client_destroy(wsClient); + wsClient = NULL; } void BlockNotify::restart() diff --git a/src/lib/block_notify.hpp b/src/lib/block_notify.hpp index aa99f2b..15aabee 100644 --- a/src/lib/block_notify.hpp +++ b/src/lib/block_notify.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include @@ -48,16 +48,15 @@ public: uint getLastBlockUpdate() const; void setLastBlockUpdate(uint lastUpdate); - // Task handling - static void taskNotify(void* pvParameters); - private: BlockNotify() = default; // Private constructor for singleton void setupTask(); - static void onWebsocketEvent(WStype_t type, uint8_t* payload, size_t length); + static void onWebsocketEvent(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); + void onWebsocketMessage(esp_websocket_event_data_t *data); - static WebSocketsClient webSocket; + static const char* mempoolWsCert; + static esp_websocket_client_handle_t wsClient; static uint32_t currentBlockHeight; static uint16_t blockMedianFee; static bool notifyInit; diff --git a/src/lib/v2_notify.cpp b/src/lib/v2_notify.cpp index a8174a1..4d23f3a 100644 --- a/src/lib/v2_notify.cpp +++ b/src/lib/v2_notify.cpp @@ -176,7 +176,7 @@ namespace V2Notify for (;;) { webSocket.loop(); - vTaskDelay(10 / portTICK_PERIOD_MS); + vTaskDelay(pdMS_TO_TICKS(10)); } }