#ifndef Socket_h #define Socket_h #include #include #include #include #include #include #define EVENT_SERVICE_PATH "/ws/events" typedef std::function EventCallback; typedef std::function SubscribeCallback; class EventSocket { public: EventSocket(PsychicHttpServer *server, SecurityManager *_securityManager, AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_AUTHENTICATED); void begin(); void onEvent(String event, EventCallback callback); void onSubscribe(String event, SubscribeCallback callback); void emit(String event, String payload); void emit(const char *event, const char *payload); void emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin = false); // if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId private: PsychicHttpServer *_server; PsychicWebSocketHandler _socket; SecurityManager *_securityManager; AuthenticationPredicate _authenticationPredicate; std::map> client_subscriptions; std::map> event_callbacks; std::map> subscribe_callbacks; void handleEventCallbacks(String event, JsonObject &jsonObject, int originId); void handleSubscribeCallbacks(String event, const String &originId); void onWSOpen(PsychicWebSocketClient *client); void onWSClose(PsychicWebSocketClient *client); esp_err_t onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame); }; #endif