Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ namespace crow
/// \brief An HTTP server that runs on SSL with an SSLAdaptor
using ssl_server_t = Server<Crow, TCPAcceptor, SSLAdaptor, Middlewares...>;
#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<Middlewares...>>;

Crow()
{}

Expand Down
37 changes: 36 additions & 1 deletion include/crow/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,41 +476,76 @@ 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<typename Func>
self_t& onopen(Func f)
{
open_handler_ = f;
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<typename Func>
self_t& onmessage(Func f)
{
message_handler_ = f;
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<typename Func>
self_t& onclose(Func f)
{
close_handler_ = f;
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<typename Func>
self_t& onerror(Func f)
{
error_handler_ = f;
return *this;
}


/// \brief Set functor that process a client request to start a WebSocket.
/// The required interface is:
/// const crow::request& conn, std::optional<crow::response>& response, void** userData)
///
/// \param callback Functor to set.
///
self_t& onaccept(std::function<void(const crow::request&, std::optional<crow::response>&, 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<bool(const crow::request&, void**)>&& callback)
{
onaccept([callback](const crow::request& req, std::optional<crow::response>& res, void** p) {
Expand Down
Loading