Skip to content

Commit

Permalink
Add ability to set User-Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Ashby committed Nov 10, 2017
1 parent d6fe6e9 commit 36d9ad5
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/scio_beast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,14 @@ class ConnectOptions {
return *this;
}

ConnectOptions& setUserAgent(const std::string& ua) {
userAgent = ua;
return *this;
}

std::string host;
std::string port;
std::string userAgent;
bool secure; // SSL/TLS?
std::string path;
bool autoReconnect;
Expand Down Expand Up @@ -1470,20 +1476,28 @@ class SCSocket
}

m_state = State::OPEN;

if(m_connectOptions.secure) {
m_wss->async_handshake(
m_connectOptions.host,
m_connectOptions.path,
std::bind(&SCSocket::initialHandshakeHandler, shared_from_this(), std::placeholders::_1)
);
performHandshake(m_wss);
} else {
m_ws->async_handshake(
m_connectOptions.host,
m_connectOptions.path,
std::bind(&SCSocket::initialHandshakeHandler, shared_from_this(), std::placeholders::_1)
);
}
performHandshake(m_ws);
}
}

template <typename NextLayer>
void performHandshake(std::unique_ptr<NextLayer>& s) {
auto self(shared_from_this());

s->async_handshake_ex(
m_connectOptions.host,
m_connectOptions.path,
[ this, self ](boost::beast::websocket::request_type& req) {
if(!m_connectOptions.userAgent.empty()) {
req.insert(boost::beast::http::field::user_agent, m_connectOptions.userAgent);
}
},
std::bind(&SCSocket::initialHandshakeHandler, shared_from_this(), std::placeholders::_1)
);
}

void initialHandshakeHandler(boost::system::error_code ec) {
Expand Down

0 comments on commit 36d9ad5

Please sign in to comment.