Skip to content

Instantly share code, notes, and snippets.

@markhc
Created August 13, 2019 17:05
Show Gist options
  • Save markhc/c15f27f807155420d3ada08847b64c20 to your computer and use it in GitHub Desktop.
Save markhc/c15f27f807155420d3ada08847b64c20 to your computer and use it in GitHub Desktop.
void fail(beast::error_code ec, char const* what)
{
// TODO: Better error logging
std::cerr << what << ": " << ec.message() << "\n";
}
// -------------------------------------------------------------------------------------------------
Gateway::Gateway(net::io_context& ioc, ssl::context& ctx)
: ws_(net::make_strand(ioc), ctx),
resolver_(net::make_strand(ioc)),
host_("gateway.discord.gg"s),
port_("443"s),
target_("/?v=6&encoding=json"s)
{
}
// -------------------------------------------------------------------------------------------------
void Gateway::async_connect()
{
ws_.next_layer().set_verify_mode(ssl::verify_none);
resolver_.async_resolve(
host_, port_, beast::bind_front_handler(&Gateway::onResolve, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onResolve(boost::system::error_code ec, tcp::resolver::results_type results)
{
if (ec) {
return fail(ec, "resolve");
}
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Make the connection on the IP address we get from a lookup
beast::get_lowest_layer(ws_).async_connect(
results, beast::bind_front_handler(&Gateway::onConnect, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onConnect(boost::system::error_code ec, tcp::resolver::results_type::endpoint_type)
{
if (ec) {
return fail(ec, "connect");
}
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake
ws_.next_layer().async_handshake(
ssl::stream_base::client,
beast::bind_front_handler(&Gateway::onSslHandshake, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onSslHandshake(beast::error_code ec)
{
if (ec) return fail(ec, "ssl_handshake");
// Turn off the timeout on the tcp_stream, because
// the websocket stream has its own timeout system.
beast::get_lowest_layer(ws_).expires_never();
// Set suggested timeout settings for the websocket
ws_.set_option(ws::stream_base::timeout::suggested(beast::role_type::client));
// Set a decorator to change the User-Agent of the handshake
ws_.set_option(ws::stream_base::decorator(
[](ws::request_type& req) { req.set(http::field::user_agent, TENSHI_USER_AGENT); }));
// Perform the websocket handshake
ws_.async_handshake(
host_, target_, beast::bind_front_handler(&Gateway::onHandshake, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onHandshake(boost::system::error_code ec)
{
if (ec) {
return fail(ec, "handshake");
}
// Send the message
ws_.async_read(buffer_, beast::bind_front_handler(&Gateway::onRead, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onRead(beast::error_code ec, std::size_t nbytes)
{
std::cout << boost::beast::buffers_to_string(buffer_.data()) << std::endl;
// ws_.async_write(buffer_, beast::bind_front_handler(&Gateway::onWrite, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
void Gateway::onWrite(beast::error_code ec, std::size_t nbytes)
{
// ws_.async_read(buffer_, beast::bind_front_handler(&Gateway::onRead, shared_from_this()));
}
// -------------------------------------------------------------------------------------------------
} // namespace tenshi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment