#include <userver/server/websocket/websocket_handler.hpp >
Base class for WebSocket handler.
Static options:
Inherits all the options from server::handlers::HandlerBase and adds the following ones:
Name Description Default value
log-level overrides log level for this handle <no override>
status-codes-log-level map of "status": log_level items to override span log level for specific status codes {}
max-remote-payload max remote payload size 65536
fragment-size max output fragment size 65536
Example usage:
namespace samples::websocket {
public :
static constexpr std::string_view kName = "websocket-handler" ;
using WebsocketHandlerBase::WebsocketHandlerBase;
chat.
Send (std::move(message));
}
}
};
}
Examples samples/websocket_service/main.cpp .
Definition at line 37 of file websocket_handler.hpp .
WebsocketHandlerBase (const components::ComponentConfig &, const components::ComponentContext &)
virtual void Handle (WebSocketConnection &websocket, server::request::RequestContext &) const =0
Websocket handler code belongs here.
virtual bool HandleHandshake (const server::http::HttpRequest &, server::http::HttpResponse &, server::request::RequestContext &) const
If any code is required for handshake validation, it goes here.
virtual void HandleNonWebsocketRequest (const server::http::HttpRequest &request, server::request::RequestContext &context) const
If request isn't a websocket request the function handles a request.
void PrepareAndHandleRequest (http::HttpRequest &request, request::RequestContext &context) const override
void ReportMalformedRequest (http::HttpRequest &request) const final
virtual const std::string & HandlerName () const
const std::vector< http::HttpMethod > & GetAllowedMethods () const
virtual logging::Level GetLogLevelForResponseStatus (http::HttpStatus status) const
virtual FormattedErrorData GetFormattedExternalErrorBody (const CustomHandlerException &exc) const
std::string GetResponseDataForLoggingChecked (const http::HttpRequest &request, request::RequestContext &context, const std::string &response_data) const
void HandleCustomHandlerException (const http::HttpRequest &request, const CustomHandlerException &ex) const
void HandleUnknownException (const http::HttpRequest &request, const std::exception &ex) const
void LogUnknownException (const std::exception &ex, std::optional< logging::Level > log_level_override={}) const
Helper function to log an unknown exception.
const std::optional< logging::Level > & GetLogLevel () const
Returns the default log level for the handler.
bool IsMonitor () const
Returns whether this is a monitoring handler.
const HandlerConfig & GetConfig () const
Returns handler config.
ComponentHealth GetComponentHealth () const override
void OnLoadingCancelled () override
void OnAllComponentsLoaded () override
void OnAllComponentsAreStopping () override
◆ ClientError
◆ ExternalBody
◆ HandlerErrorCode
◆ InternalMessage
◆ InternalServerError
◆ LoggableComponentBase
◆ GetComponentHealth()
ComponentHealth components::ComponentBase::GetComponentHealth
(
)
const
inline override virtual inherited
◆ GetLogLevelForResponseStatus()
Override it if you need a custom logging level for messages about finish of request handling for some http statuses.
◆ GetRequestBodyForLogging()
protected virtual inherited
◆ GetResponseDataForLogging()
protected virtual inherited
◆ HandleCustomHandlerException()
Takes the exception and formats it into response, as specified by exception.
◆ HandleHandshake()
If any code is required for handshake validation, it goes here.
Definition at line 47 of file websocket_handler.hpp .
◆ HandleNonWebsocketRequest()
If request isn't a websocket request the function handles a request.
Definition at line 60 of file websocket_handler.hpp .
◆ HandleRequest()
protected virtual inherited
The core method for HTTP request handling. request
arg contains HTTP headers, full body, etc. The method should return response body.
Note It is used only if IsStreamed() returned false
.
Examples samples/http_caching/main.cpp .
◆ HandleStreamRequest()
protected virtual inherited
The core method for HTTP request handling. request
arg contains HTTP headers, full body, etc. The response body is passed in parts to ResponseBodyStream
. Stream transmission is useful when: 1) The body size is unknown beforehand. 2) The client may take advantage of early body transmission (e.g. a Web Browser may start rendering the HTML page or downloading dependant resources). 3) The body size is huge and we want to have only a part of it in memory.
Note It is used only if IsStreamed() returned true
.
◆ HandleUnknownException()
void server::handlers::HttpHandlerBase::HandleUnknownException
(
const http::HttpRequest &
request ,
const std::exception &
ex
)
const
inherited
Takes the exception and formats it into response as an internal server error.
◆ IsMethodStatisticIncluded()
virtual bool server::handlers::HttpHandlerBase::IsMethodStatisticIncluded
(
)
const
inline protected virtual inherited
Override it to show per HTTP-method statistics besides statistics for all methods
Definition at line 161 of file http_handler_base.hpp .
◆ IsMonitor()
bool server::handlers::HandlerBase::IsMonitor
(
)
const
inline inherited
Returns whether this is a monitoring handler.
Definition at line 67 of file handler_base.hpp .
◆ IsStreamed() [1/2]
virtual bool server::handlers::HttpHandlerBase::IsStreamed
(
)
const
inline protected virtual inherited
◆ IsStreamed() [2/2]
inline protected virtual inherited
Override it if you need a custom streamed logic based on request and context.
Note The default implementation returns the cached value of "response-body-streamed" value from static config.
Definition at line 157 of file http_handler_base.hpp .
◆ NeedCheckAuth()
virtual bool server::handlers::HttpHandlerBase::NeedCheckAuth
(
)
const
inline protected virtual inherited
Override it if you want to disable auth checks in handler by some condition
Definition at line 165 of file http_handler_base.hpp .
◆ OnAllComponentsAreStopping()
void components::ComponentBase::OnAllComponentsAreStopping
(
)
inline override virtual inherited
◆ OnAllComponentsLoaded()
void components::ComponentBase::OnAllComponentsLoaded
(
)
inline override virtual inherited
◆ OnLoadingCancelled()
void components::ComponentBase::OnLoadingCancelled
(
)
inline override virtual inherited
Called once if the creation of any other component failed. If the current component expects some other component to take any action with the current component, this call is a signal that such action may never happen due to components loading was cancelled. Application components might not want to override it.
Reimplemented from components::RawComponentBase .
Definition at line 42 of file component_base.hpp .
◆ ParseRequestData()
inline protected virtual inherited
◆ PrepareAndHandleRequest()
◆ ReportMalformedRequest()
void server::handlers::HttpHandlerBase::ReportMalformedRequest
(
http::HttpRequest &
)
const
final virtual inherited
Produces response to a request unrecognized by the protocol based on provided generic response. Does not throw.
Reimplemented from server::handlers::HandlerBase .
The documentation for this class was generated from the following file: