diff --git a/src/main.cpp b/src/main.cpp
index 47d63a6..97dbd39 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -15,8 +15,7 @@
* along with duckticker. If not, see .
*/
-#include
-#include
+#include "websocket_reader.hpp"
#include
#include
#include
@@ -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 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"})"
};
diff --git a/src/meson.build b/src/meson.build
index 0dae7c4..67a8cd4 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -23,6 +23,7 @@ executable(meson.project_name(),
'nap/http_header_parse.cpp',
'nap/page_fetch.cpp',
'nap/quick_rest.cpp',
+ 'websocket_reader.cpp',
install: true,
dependencies: [
curlcpp_dep,
diff --git a/src/websocket_reader.cpp b/src/websocket_reader.cpp
new file mode 100644
index 0000000..1c7e078
--- /dev/null
+++ b/src/websocket_reader.cpp
@@ -0,0 +1,69 @@
+/* Copyright 2022, Michele Santullo
+ * This file is part of duckticker.
+ *
+ * Wrenpp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Wrenpp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with duckticker. If not, see .
+ */
+
+#include "websocket_reader.hpp"
+#include
+#include
+
+namespace duck {
+const std::string_view WebsocketReader::BeastVersionString = BOOST_BEAST_VERSION_STRING;
+
+struct WebsocketReader::LocalData {
+ using tcp = boost::asio::ip::tcp;
+
+ LocalData() :
+ resolver{ioc},
+ ws{ioc}
+ { }
+
+ boost::asio::io_context ioc;
+ tcp::resolver resolver;
+ boost::beast::websocket::stream ws;
+};
+
+WebsocketReader::WebsocketReader (std::string host,
+ const std::string& port,
+ const std::string& path,
+ const std::string& user_agent,
+ const std::string& message
+) :
+ m_local(std::make_unique())
+{
+ //see https://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp
+ const auto results = m_local->resolver.resolve(host, port);
+ auto ep = boost::asio::connect(m_local->ws.next_layer(), results);
+ host += ':' + std::to_string(ep.port());
+ m_local->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_local->ws.handshake(host, path);
+ m_local->ws.write(boost::asio::buffer(message));
+}
+
+std::string WebsocketReader::read() {
+ boost::beast::flat_buffer buffer;
+ m_local->ws.read(buffer);
+ return boost::beast::buffers_to_string(buffer.data());
+}
+
+WebsocketReader::~WebsocketReader() {
+ m_local->ws.close(boost::beast::websocket::close_code::normal);
+}
+} //namespace duck
diff --git a/src/websocket_reader.hpp b/src/websocket_reader.hpp
new file mode 100644
index 0000000..d18e984
--- /dev/null
+++ b/src/websocket_reader.hpp
@@ -0,0 +1,45 @@
+/* Copyright 2022, Michele Santullo
+ * This file is part of duckticker.
+ *
+ * Wrenpp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Wrenpp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with duckticker. If not, see .
+ */
+
+#pragma once
+
+#include
+#include
+#include
+
+namespace duck {
+class WebsocketReader {
+public:
+ static const std::string_view BeastVersionString;
+
+ 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:
+ struct LocalData;
+ std::unique_ptr m_local;
+};
+
+} //namespace duck