/** * * WebSocketController.h * An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include #include #include #include #include #include #include #include #include #define WS_PATH_LIST_BEGIN \ static void initPathRouting() \ { #define WS_PATH_ADD(path, ...) registerSelf__(path, {__VA_ARGS__}) #define WS_PATH_LIST_END } namespace drogon { /** * @brief The abstract base class for WebSocket controllers. * */ class WebSocketControllerBase : public virtual DrObjectBase { public: // This function is called when a new message is received virtual void handleNewMessage(const WebSocketConnectionPtr &, std::string &&, const WebSocketMessageType &) = 0; // This function is called after a new connection of WebSocket is // established. virtual void handleNewConnection(const HttpRequestPtr &, const WebSocketConnectionPtr &) = 0; // This function is called after a WebSocket connection is closed virtual void handleConnectionClosed(const WebSocketConnectionPtr &) = 0; virtual ~WebSocketControllerBase() { } }; using WebSocketControllerBasePtr = std::shared_ptr; /** * @brief The reflection base class template for WebSocket controllers * * @tparam T the type of the implementation class * @tparam AutoCreation The flag for automatically creating, user can set this * flag to false for classes that have nondefault constructors. */ template class WebSocketController : public DrObject, public WebSocketControllerBase { public: static const bool isAutoCreation = AutoCreation; virtual ~WebSocketController() { } protected: WebSocketController() { } static void registerSelf__( const std::string &path, const std::vector &filtersAndMethods) { LOG_TRACE << "register websocket controller(" << WebSocketController::classTypeName() << ") on path:" << path; app().registerWebSocketController( path, WebSocketController::classTypeName(), filtersAndMethods); } private: class pathRegistrator { public: pathRegistrator() { if (AutoCreation) { T::initPathRouting(); } } }; friend pathRegistrator; static pathRegistrator registrator_; virtual void *touch() { return ®istrator_; } }; template typename WebSocketController::pathRegistrator WebSocketController::registrator_; } // namespace drogon