Refactor WebsocketReader into a separate file
This commit is contained in:
parent
10fc605078
commit
2016f19c3d
4 changed files with 118 additions and 56 deletions
59
src/main.cpp
59
src/main.cpp
|
@ -15,8 +15,7 @@
|
|||
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include "websocket_reader.hpp"
|
||||
#include <simdjson.h>
|
||||
#include <wrenpp/vm_fun.hpp>
|
||||
#include <wrenpp/def_configuration.hpp>
|
||||
|
@ -54,58 +53,6 @@ class App {
|
|||
var the_app = App.new("USD", "kraken")
|
||||
)script";
|
||||
|
||||
class WebsocketReader {
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
public:
|
||||
WebsocketReader (std::string host,
|
||||
const std::string& port,
|
||||
const std::string& path,
|
||||
const std::string& user_agent,
|
||||
const std::string& message
|
||||
);
|
||||
|
||||
~WebsocketReader();
|
||||
|
||||
std::string read();
|
||||
|
||||
private:
|
||||
boost::asio::io_context m_ioc;
|
||||
tcp::resolver m_resolver;
|
||||
boost::beast::websocket::stream<tcp::socket> m_ws;
|
||||
};
|
||||
|
||||
WebsocketReader::WebsocketReader (std::string host,
|
||||
const std::string& port,
|
||||
const std::string& path,
|
||||
const std::string& user_agent,
|
||||
const std::string& message
|
||||
) :
|
||||
m_resolver{m_ioc},
|
||||
m_ws{m_ioc}
|
||||
{
|
||||
//see https://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp
|
||||
const auto results = m_resolver.resolve(host, port);
|
||||
auto ep = boost::asio::connect(m_ws.next_layer(), results);
|
||||
host += ':' + std::to_string(ep.port());
|
||||
m_ws.set_option(boost::beast::websocket::stream_base::decorator(
|
||||
[user_agent](boost::beast::websocket::request_type& req)
|
||||
{
|
||||
req.set(boost::beast::http::field::user_agent, user_agent);
|
||||
}
|
||||
));
|
||||
m_ws.handshake(host, path);
|
||||
m_ws.write(boost::asio::buffer(message));
|
||||
}
|
||||
|
||||
std::string WebsocketReader::read() {
|
||||
boost::beast::flat_buffer buffer;
|
||||
m_ws.read(buffer);
|
||||
return boost::beast::buffers_to_string(buffer.data());
|
||||
}
|
||||
|
||||
WebsocketReader::~WebsocketReader() {
|
||||
m_ws.close(boost::beast::websocket::close_code::normal);
|
||||
}
|
||||
|
||||
class TickerPrice {
|
||||
public:
|
||||
|
@ -133,10 +80,10 @@ private:
|
|||
TickerPrice ticker_price_bitoinity (std::string_view currency, std::string_view exchange) {
|
||||
static const TickerPrice error_price {0.0, "", ""};
|
||||
|
||||
WebsocketReader websocket{"bitcoinity.org",
|
||||
duck::WebsocketReader websocket{"bitcoinity.org",
|
||||
"80",
|
||||
"/webs_bridge/websocket",
|
||||
std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-coro",
|
||||
std::string{duck::WebsocketReader::BeastVersionString} + " websocket-client-coro",
|
||||
R"({"topic":"webs:markets_)" + std::string{exchange} + "_" +
|
||||
std::string{currency} + R"(","event":"phx_join","payload":{},"ref":"3"})"
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue