userver: clients::http::WebSocketResponse Class Reference
Loading...
Searching...
No Matches
clients::http::WebSocketResponse Class Referencefinal

#include <userver/clients/http/websocket_response.hpp>

Detailed Description

HTTP response for WebSocket upgrade.

Call http::client::Request::PerformWebSocketHandshake() to get one. After successful WebSocket handshake, you can use MakeWebSocketConnection() to establish a WebSocket connection.

class WebSocketClientHandler final : public server::handlers::HttpHandlerBase {
public:
static constexpr std::string_view kName = "handler-websocket-client";
WebSocketClientHandler(const components::ComponentConfig& config, const components::ComponentContext& context)
: HttpHandlerBase(config, context),
http_client_(context.FindComponent<components::HttpClient>().GetHttpClient())
{}
std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&)
const override {
const auto& ws_server_url = request.GetArg("url");
// Perform WebSocket handshake
auto ws_response = http_client_.CreateRequest().url(ws_server_url).PerformWebSocketHandshake();
// Create WebSocket connection
auto conn = ws_response.MakeWebSocketConnection();
conn->SendText(request.GetArg("message"));
// Receive response
websocket::Message response;
conn->Recv(response);
// Close connection
conn->Close(websocket::CloseStatus::kNormal);
return response.data;
}
private:
clients::http::Client& http_client_;
};

Definition at line 24 of file websocket_response.hpp.

Public Member Functions

 WebSocketResponse (WebSocketResponse &&)=default
 
 WebSocketResponse (const WebSocketResponse &)=delete
 
WebSocketResponseoperator= (WebSocketResponse &&)=default
 
WebSocketResponseoperator= (const WebSocketResponse &)=delete
 
std::shared_ptr< ResponseGetHandshakeResponse ()
 Get the HTTP handshake response.
 
bool IsProtocolUpgraded () const
 Check if WebSocket protocol upgrade was successful.
 
std::shared_ptr< websocket::WebSocketConnectionMakeWebSocketConnection ()
 Create a WebSocket connection from this response.
 

Member Function Documentation

◆ GetHandshakeResponse()

std::shared_ptr< Response > clients::http::WebSocketResponse::GetHandshakeResponse ( )
inline

Get the HTTP handshake response.

Definition at line 37 of file websocket_response.hpp.

◆ IsProtocolUpgraded()

bool clients::http::WebSocketResponse::IsProtocolUpgraded ( ) const

Check if WebSocket protocol upgrade was successful.

Returns
true if handshake completed with status 101 Switching Protocols

The documentation for this class was generated from the following file: