🎨 Pull subscribe logic out from websocket

This commit is contained in:
Rune Harlyk
2025-09-14 22:25:57 +02:00
committed by Rune Harlyk
parent 06d27e0644
commit 1cadcf8bdb
4 changed files with 183 additions and 158 deletions
@@ -8,23 +8,16 @@
#include <vector>
#include <string>
enum message_type_t { CONNECT = 0, DISCONNECT = 1, EVENT = 2, PING = 3, PONG = 4, BINARY_EVENT = 5 };
#include <communication/comm_base.hpp>
typedef std::function<void(JsonVariant &root, int originId)> EventCallback;
typedef std::function<void(const std::string &originId, bool sync)> SubscribeCallback;
class EventSocket {
class Websocket : CommAdapterBase {
public:
EventSocket(PsychicHttpServer &server, const char *route = "/api/ws");
Websocket(PsychicHttpServer &server, const char *route = "/api/ws");
void begin();
bool hasSubscribers(const char *event);
void begin() override;
void onEvent(std::string event, EventCallback callback);
void onSubscribe(std::string event, SubscribeCallback callback);
void emit(const char *event, JsonVariant &payload, const char *originId = "", bool onlyToSameOrigin = false);
private:
@@ -32,16 +25,11 @@ class EventSocket {
PsychicHttpServer &_server;
const char *_route;
std::map<std::string, std::list<int>> client_subscriptions;
std::map<std::string, std::list<EventCallback>> event_callbacks;
std::map<std::string, std::list<SubscribeCallback>> subscribe_callbacks;
void handleEventCallbacks(std::string event, JsonVariant &jsonObject, int originId);
void send(PsychicWebSocketClient *client, const char *data, size_t len);
void handleSubscribeCallbacks(std::string event, const std::string &originId);
void onWSOpen(PsychicWebSocketClient *client);
void onWSClose(PsychicWebSocketClient *client);
esp_err_t onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame);
void send(const uint8_t *data, size_t len, int cid = -1) override;
};
#endif