drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
WebSocketController.h
1
14
15#pragma once
16
17#include <drogon/DrObject.h>
20#include <drogon/HttpTypes.h>
21#include <trantor/utils/Logger.h>
22#include <iostream>
23#include <memory>
24#include <string>
25#include <vector>
26
27#define WS_PATH_LIST_BEGIN \
28 static void initPathRouting() \
29 {
30#define WS_PATH_ADD(path, ...) registerSelf__(path, {__VA_ARGS__})
31#define WS_ADD_PATH_VIA_REGEX(regExp, ...) \
32 registerSelfRegex__(regExp, {__VA_ARGS__})
33#define WS_PATH_LIST_END }
34
35namespace drogon
36{
42{
43 public:
44 // This function is called when a new message is received
45 virtual void handleNewMessage(const WebSocketConnectionPtr &,
46 std::string &&,
47 const WebSocketMessageType &) = 0;
48
49 // This function is called after a new connection of WebSocket is
50 // established.
51 virtual void handleNewConnection(const HttpRequestPtr &,
52 const WebSocketConnectionPtr &) = 0;
53
54 // This function is called after a WebSocket connection is closed
55 virtual void handleConnectionClosed(const WebSocketConnectionPtr &) = 0;
56
58 {
59 }
60};
61
62using WebSocketControllerBasePtr = std::shared_ptr<WebSocketControllerBase>;
63
71template <typename T, bool AutoCreation = true>
72class WebSocketController : public DrObject<T>, public WebSocketControllerBase
73{
74 public:
75 static const bool isAutoCreation = AutoCreation;
76
77 virtual ~WebSocketController()
78 {
79 }
80
81 protected:
82 WebSocketController()
83 {
84 }
85
86 static void registerSelf__(
87 const std::string &path,
88 const std::vector<internal::HttpConstraint> &constraints)
89 {
90 LOG_TRACE << "register websocket controller("
91 << WebSocketController<T, AutoCreation>::classTypeName()
92 << ") on path:" << path;
94 path,
95 WebSocketController<T, AutoCreation>::classTypeName(),
96 constraints);
97 }
98
99 static void registerSelfRegex__(
100 const std::string &regExp,
101 const std::vector<internal::HttpConstraint> &constraints)
102 {
103 LOG_TRACE << "register websocket controller("
104 << WebSocketController<T, AutoCreation>::classTypeName()
105 << ") on regExp:" << regExp;
107 regExp,
108 WebSocketController<T, AutoCreation>::classTypeName(),
109 constraints);
110 }
111
112 private:
113 class pathRegistrator
114 {
115 public:
116 pathRegistrator()
117 {
118 if (AutoCreation)
119 {
120 T::initPathRouting();
121 }
122 }
123 };
124
125 friend pathRegistrator;
126 static pathRegistrator registrator_;
127
128 virtual void *touch()
129 {
130 return &registrator_;
131 }
132};
133
134template <typename T, bool AutoCreation>
135typename WebSocketController<T, AutoCreation>::pathRegistrator
136 WebSocketController<T, AutoCreation>::registrator_;
137
138} // namespace drogon
The base class for all drogon reflection classes.
Definition DrObject.h:34
virtual HttpAppFramework & registerWebSocketController(const std::string &pathName, const std::string &ctrlName, const std::vector< internal::HttpConstraint > &constraints={})=0
Register a WebSocketController into the framework.
virtual HttpAppFramework & registerWebSocketControllerRegex(const std::string &regExp, const std::string &ctrlName, const std::vector< internal::HttpConstraint > &constraints=std::vector< internal::HttpConstraint >{})=0
Register a WebSocketController into the framework.
The abstract base class for WebSocket controllers.
Definition WebSocketController.h:42
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
HttpAppFramework & app()
A wrapper of the instance() method.
Definition HttpAppFramework.h:1656