From e7c74917191a4953d495295b65732aa3549ba753 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 19 Oct 2025 00:12:50 +0200 Subject: Add new module websocket and use it Implement /api/v1/events which will send out messages when things change, such as the main controller. --- src/websocket.hh | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/websocket.hh (limited to 'src/websocket.hh') diff --git a/src/websocket.hh b/src/websocket.hh new file mode 100644 index 0000000..f7a99f1 --- /dev/null +++ b/src/websocket.hh @@ -0,0 +1,88 @@ +#ifndef WEBSOCKET_HH +#define WEBSOCKET_HH + +#include +#include +#include + +namespace logger { +class Logger; +} // namespace logger + +namespace looper { +class Looper; +} // namespace looper + +namespace cfg { +class Config; +} // namespace cfg + +namespace http { +class Response; +class Request; +} // namespace http + +namespace ws { + +class Message { + public: + virtual ~Message() = default; + + [[nodiscard]] + virtual bool is_text() const = 0; + + [[nodiscard]] + virtual bool is_binary() const = 0; + + [[nodiscard]] + virtual std::string_view text() const = 0; + + [[nodiscard]] + virtual std::span binary() const = 0; + + static std::unique_ptr create(std::string_view text); + static std::unique_ptr create(std::span data); + + protected: + Message() = default; + Message(Message const&) = delete; + Message& operator=(Message const&) = delete; +}; + +class Server { + public: + virtual ~Server() = default; + + class Delegate { + public: + virtual ~Delegate() = default; + + virtual std::unique_ptr handle(Message const& msg) = 0; + + protected: + Delegate() = default; + }; + + virtual std::unique_ptr handle( + http::Request const& request) = 0; + + virtual void send_text_to_all(std::string_view text); + virtual void send_binary_to_all(std::span data); + virtual void send_to_all(Message const& message) = 0; + + protected: + Server() = default; + + private: + Server(Server const&) = delete; + Server& operator=(Server const&) = delete; +}; + +std::unique_ptr create_server(logger::Logger& logger, + cfg::Config const& cfg, + looper::Looper& looper, + Server::Delegate& delegate); + +} // namespace ws + +#endif // WEBSOCKET_HH -- cgit v1.2.3-70-g09d2