userver: userver/server/websocket/websocket_handler.hpp Source File
Loading...
Searching...
No Matches
websocket_handler.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/websocket/websocket_handler.hpp
4/// @brief @copybrief websocket::WebsocketHandlerBase
5
6#include <userver/components/component_config.hpp>
7#include <userver/components/component_context.hpp>
8#include <userver/server/handlers/http_handler_base.hpp>
9#include <userver/server/websocket/server.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace server::websocket {
14
15// clang-format off
16
17/// @ingroup userver_components userver_http_handlers userver_base_classes
18///
19/// @brief Base class for WebSocket handler
20///
21/// ## Static options:
22/// Inherits all the options from server::handlers::HandlerBase and adds the
23/// following ones:
24///
25/// Name | Description | Default value
26/// ---- | ----------- | -------------
27/// log-level | overrides log level for this handle | <no override>
28/// status-codes-log-level | map of "status": log_level items to override span log level for specific status codes | {}
29/// max-remote-payload | max remote payload size | 65536
30/// fragment-size | max output fragment size | 65536
31///
32/// ## Example usage:
33///
34/// @snippet samples/websocket_service/websocket_service.cpp Websocket service sample - component
35
36// clang-format on
37class WebsocketHandlerBase : public server::handlers::HttpHandlerBase {
38 public:
39 WebsocketHandlerBase(const components::ComponentConfig&,
40 const components::ComponentContext&);
41
42 /// @brief Websocket handler code belongs here.
43 virtual void Handle(WebSocketConnection& websocket,
44 server::request::RequestContext&) const = 0;
45
46 /// @brief If any code is required for handshake validation,
47 /// it goes here.
48 virtual bool HandleHandshake(const server::http::HttpRequest&,
49 server::http::HttpResponse&,
50 server::request::RequestContext&) const {
51 return true;
52 }
53
54 /// @cond
55 void WriteMetrics(utils::statistics::Writer& writer) const;
56
57 static yaml_config::Schema GetStaticConfigSchema();
58 /// @endcond
59
60 private:
61 std::string HandleRequestThrow(
62 const server::http::HttpRequest& request,
63 server::request::RequestContext& context) const override;
64
65 websocket::Config config_;
66 mutable Statistics stats_;
67 utils::statistics::Entry statistics_holder_;
68};
69
70} // namespace server::websocket
71
72USERVER_NAMESPACE_END