Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Release/include/cpprest/ws_client.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -150,6 +150,12 @@ class websocket_client_config
return m_sni_hostname;
}

/// <summary>
/// Sets the User Agent to be used for the connection
/// </summary>
/// <param name="name">The User Agent to use, as a string.</param>
_ASYNCRTIMP void set_user_agent(const utf8string &user_agent);

/// <summary>
/// Gets the headers of the HTTP request message used in the WebSocket protocol handshake.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion Release/src/websockets/client/ws_client_wspp.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -312,6 +312,15 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
shutdown_wspp_impl<WebsocketConfigType>(con_hdl, false);
});

// Set User Agent specified by the user. This needs to happen before any connection is created
const auto& headers = m_config.headers();

auto user_agent_it = headers.find(web::http::header_names::user_agent);
if (user_agent_it != headers.end())
{
client.set_user_agent(utility::conversions::to_utf8string(user_agent_it->second));
}

// Get the connection handle to save for later, have to create temporary
// because type erasure occurs with connection_hdl.
websocketpp::lib::error_code ec;
Expand All@@ -323,7 +332,6 @@ class wspp_callback_client : public websocket_client_callback_impl, public std::
}

// Add any request headers specified by the user.
const auto & headers = m_config.headers();
for (const auto & header : headers)
{
if (!utility::details::str_icmp(header.first, g_subProtocolHeader))
Expand Down
6 changes: 6 additions & 0 deletions Release/src/websockets/client/ws_msg.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,12 @@ namespace client
{

static ::utility::string_t g_subProtocolHeader = _XPLATSTR("Sec-WebSocket-Protocol");

void websocket_client_config::set_user_agent(const utf8string &user_agent)
{
headers().add(web::http::header_names::user_agent, utility::conversions::to_string_t(user_agent));
}

void websocket_client_config::add_subprotocol(const ::utility::string_t &name)
{
m_headers.add(g_subProtocolHeader, name);
Expand Down