Skip to content

Commit 21f8bcf

Browse files
authored
in crow::Crow<> : add definition of WebSocketRule_t to simplify usage of CROW_WEBSOCKET_ROUTE
1 parent c61a26e commit 21f8bcf

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

include/crow/app.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,32 @@ namespace crow
224224
/// \brief An HTTP server that runs on SSL with an SSLAdaptor
225225
using ssl_server_t = Server<Crow, TCPAcceptor, SSLAdaptor, Middlewares...>;
226226
#endif
227+
/// \brief WebSocket rule type used in this application.
228+
///
229+
/// Usefull during WebSocket route definition.
230+
/// Usage:
231+
///
232+
/// ```cpp
233+
/// crow::SimpleApp::WebSocketRule_t& ws = CROW_WEBSOCKET_ROUTE(app, "/ws");
234+
///
235+
/// ws.onaccept([](const crow::request& /*conn*/, void** userData) -> bool
236+
/// {
237+
/// // ...
238+
/// return true;
239+
/// });
240+
/// ws.onopen([](crow::websocket::connection& conn) {
241+
/// // ...
242+
/// });
243+
/// ws.onclose([](crow::websocket::connection& conn, const std::string& /*reason*/, uint16_t){
244+
/// // ...
245+
/// });
246+
/// ws.onmessage([](crow::websocket::connection& conn, const std::string& msgData, bool is_binary) {
247+
/// // ...
248+
/// });
249+
/// ```
250+
///
251+
using WebSocketRule_t = WebSocketRule<Crow<Middlewares...>>;
252+
227253
Crow()
228254
{}
229255

include/crow/routing.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,41 +476,76 @@ namespace crow // NOTE: Already documented in "crow/app.h"
476476
return *this;
477477
}
478478

479+
/// \brief Set functor that process a client request to open a WebSocket.
480+
/// The required interface is:
481+
/// void(crow::websocket::connection& conn)
482+
///
483+
/// \param f Functor to set.
484+
///
479485
template<typename Func>
480486
self_t& onopen(Func f)
481487
{
482488
open_handler_ = f;
483489
return *this;
484490
}
485491

492+
/// \brief Set functor that process a client message.
493+
/// The required interface is:
494+
/// void(crow::websocket::connection& conn, const std::string& msgData, bool is_binary)
495+
///
496+
/// \param f Functor to set.
497+
///
486498
template<typename Func>
487499
self_t& onmessage(Func f)
488500
{
489501
message_handler_ = f;
490502
return *this;
491503
}
492504

505+
/// \brief Set functor that process a client close.
506+
/// The required interface is:
507+
/// void(crow::websocket::connection& conn, const std::string& reason, uint16_t status_code)
508+
///
509+
/// \param f Functor to set.
510+
///
493511
template<typename Func>
494512
self_t& onclose(Func f)
495513
{
496514
close_handler_ = f;
497515
return *this;
498516
}
499517

518+
/// \brief Set functor that process an error on this WebSocket.
519+
/// The required interface is:
520+
/// void(crow::websocket::connection& conn, const std::string& error_message)
521+
///
522+
/// \param f Functor to set.
523+
///
500524
template<typename Func>
501525
self_t& onerror(Func f)
502526
{
503527
error_handler_ = f;
504528
return *this;
505529
}
506530

507-
531+
/// \brief Set functor that process a client request to start a WebSocket.
532+
/// The required interface is:
533+
/// const crow::request& conn, std::optional<crow::response>& response, void** userData)
534+
///
535+
/// \param callback Functor to set.
536+
///
508537
self_t& onaccept(std::function<void(const crow::request&, std::optional<crow::response>&, void**)>&& callback)
509538
{
510539
accept_handler_ = std::move(callback);
511540
return *this;
512541
}
513542

543+
/// \brief Set functor that process a client request to start a WebSocket.
544+
/// The required interface is (**without response**):
545+
/// const crow::request& conn, void** userData)
546+
///
547+
/// \param callback Functor to set.
548+
///
514549
self_t& onaccept(std::function<bool(const crow::request&, void**)>&& callback)
515550
{
516551
onaccept([callback](const crow::request& req, std::optional<crow::response>& res, void** p) {

0 commit comments

Comments
 (0)