#pragma once #include "baserule.hpp" #include "http_request.hpp" #include "http_response.hpp" #include "server_sent_event.hpp" #include #include #include #include namespace crow { class SseSocketRule : public BaseRule { using self_t = SseSocketRule; public: explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) {} void validate() override {} void handle(const Request& /*req*/, const std::shared_ptr& asyncResp, const std::vector& /*params*/) override { asyncResp->res.result(boost::beast::http::status::not_found); } void handleUpgrade(const Request& /*req*/, const std::shared_ptr& /*asyncResp*/, boost::asio::ip::tcp::socket&& adaptor) override { std::shared_ptr< crow::sse_socket::ConnectionImpl> myConnection = std::make_shared< crow::sse_socket::ConnectionImpl>( std::move(adaptor), openHandler, closeHandler); myConnection->start(); } void handleUpgrade(const Request& /*req*/, const std::shared_ptr& /*asyncResp*/, boost::beast::ssl_stream&& adaptor) override { std::shared_ptr>> myConnection = std::make_shared>>( std::move(adaptor), openHandler, closeHandler); myConnection->start(); } template self_t& onopen(Func f) { openHandler = f; return *this; } template self_t& onclose(Func f) { closeHandler = f; return *this; } private: std::function openHandler; std::function closeHandler; }; } // namespace crow