diff --git a/include/crow/app.h b/include/crow/app.h index 923d91dd7..a74f7d9be 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -209,6 +209,32 @@ namespace crow /// \brief An HTTP server that runs on SSL with an SSLAdaptor using ssl_server_t = Server; #endif + /// \brief WebSocket rule type used in this application. + /// + /// Usefull during WebSocket route definition. + /// Usage: + /// + /// ```cpp + /// crow::SimpleApp::WebSocketRule_t& ws = CROW_WEBSOCKET_ROUTE(app, "/ws"); + /// + /// ws.onaccept([](const crow::request& /*conn*/, void** userData) -> bool + /// { + /// // ... + /// return true; + /// }); + /// ws.onopen([](crow::websocket::connection& conn) { + /// // ... + /// }); + /// ws.onclose([](crow::websocket::connection& conn, const std::string& /*reason*/, uint16_t){ + /// // ... + /// }); + /// ws.onmessage([](crow::websocket::connection& conn, const std::string& msgData, bool is_binary) { + /// // ... + /// }); + /// ``` + /// + using WebSocketRule_t = WebSocketRule>; + Crow() {} diff --git a/include/crow/routing.h b/include/crow/routing.h index da8c20834..7d62259ad 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -476,6 +476,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" return *this; } + /// \brief Set functor that process a client request to open a WebSocket. + /// The required interface is: + /// void(crow::websocket::connection& conn) + /// + /// \param f Functor to set. + /// template self_t& onopen(Func f) { @@ -483,6 +489,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" return *this; } + /// \brief Set functor that process a client message. + /// The required interface is: + /// void(crow::websocket::connection& conn, const std::string& msgData, bool is_binary) + /// + /// \param f Functor to set. + /// template self_t& onmessage(Func f) { @@ -490,6 +502,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" return *this; } + /// \brief Set functor that process a client close. + /// The required interface is: + /// void(crow::websocket::connection& conn, const std::string& reason, uint16_t status_code) + /// + /// \param f Functor to set. + /// template self_t& onclose(Func f) { @@ -497,6 +515,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" return *this; } + /// \brief Set functor that process an error on this WebSocket. + /// The required interface is: + /// void(crow::websocket::connection& conn, const std::string& error_message) + /// + /// \param f Functor to set. + /// template self_t& onerror(Func f) { @@ -504,13 +528,24 @@ namespace crow // NOTE: Already documented in "crow/app.h" return *this; } - + /// \brief Set functor that process a client request to start a WebSocket. + /// The required interface is: + /// const crow::request& conn, std::optional& response, void** userData) + /// + /// \param callback Functor to set. + /// self_t& onaccept(std::function&, void**)>&& callback) { accept_handler_ = std::move(callback); return *this; } + /// \brief Set functor that process a client request to start a WebSocket. + /// The required interface is (**without response**): + /// const crow::request& conn, void** userData) + /// + /// \param callback Functor to set. + /// self_t& onaccept(std::function&& callback) { onaccept([callback](const crow::request& req, std::optional& res, void** p) {