aboutsummaryrefslogtreecommitdiff
path: root/src/client/websocketpp_client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/websocketpp_client.cpp')
-rw-r--r--src/client/websocketpp_client.cpp193
1 files changed, 83 insertions, 110 deletions
diff --git a/src/client/websocketpp_client.cpp b/src/client/websocketpp_client.cpp
index 9967d74..3cc6c99 100644
--- a/src/client/websocketpp_client.cpp
+++ b/src/client/websocketpp_client.cpp
@@ -1,132 +1,105 @@
-#include "web_socket_client.hpp"
-
-using std::error_code;
-using std::function;
-using std::lock_guard;
-using std::make_tuple;
-using std::mutex;
-using std::string;
-using std::tuple;
-using std::unordered_map;
-
-namespace nostr
-{
-namespace client
-{
-/**
- * @brief An implementation of the `IWebSocketClient` interface that uses the WebSocket++ library.
- */
-class WebsocketppClient : public IWebSocketClient
-{
-public:
- void start() override
- {
- this->_client.init_asio();
- this->_client.start_perpetual();
- };
-
- void stop() override
- {
- this->_client.stop_perpetual();
- this->_client.stop();
- };
+#include "websocketpp_client.hpp"
- void openConnection(string uri) override
- {
- error_code error;
- websocketpp_client::connection_ptr connection = this->_client.get_connection(uri, error);
-
- if (error.value() == -1)
- {
- // PLOG_ERROR << "Error connecting to relay " << relay << ": " << error.message();
- }
+using namespace std;
- // Configure the connection here via the connection pointer.
- connection->set_fail_handler([this, uri](auto handle) {
- // PLOG_ERROR << "Error connecting to relay " << relay << ": Handshake failed.";
- lock_guard<mutex> lock(this->_propertyMutex);
- if (this->isConnected(uri))
- {
- this->_connectionHandles.erase(uri);
- }
- });
+void nostr::client::WebsocketppClient::start()
+{
+ this->_client.init_asio();
+ this->_client.start_perpetual();
+};
- lock_guard<mutex> lock(this->_propertyMutex);
- this->_connectionHandles[uri] = connection->get_handle();
- this->_client.connect(connection);
- };
+void nostr::client::WebsocketppClient::stop()
+{
+ this->_client.stop_perpetual();
+ this->_client.stop();
+};
- bool isConnected(string uri) override
- {
- lock_guard<mutex> lock(this->_propertyMutex);
- return this->_connectionHandles.find(uri) != this->_connectionHandles.end();
- };
+void nostr::client::WebsocketppClient::openConnection(string uri)
+{
+ error_code error;
+ websocketpp_client::connection_ptr connection = this->_client.get_connection(uri, error);
- tuple<string, bool> send(string message, string uri) override
+ if (error.value() == -1)
{
- error_code error;
+ // PLOG_ERROR << "Error connecting to relay " << relay << ": " << error.message();
+ }
- // Make sure the connection isn't closed from under us.
+ // Configure the connection here via the connection pointer.
+ connection->set_fail_handler([this, uri](auto handle) {
+ // PLOG_ERROR << "Error connecting to relay " << relay << ": Handshake failed.";
lock_guard<mutex> lock(this->_propertyMutex);
- this->_client.send(
- this->_connectionHandles[uri],
- message,
- websocketpp::frame::opcode::text,
- error);
-
- if (error.value() == -1)
+ if (this->isConnected(uri))
{
- return make_tuple(uri, false);
+ this->_connectionHandles.erase(uri);
}
+ });
- return make_tuple(uri, true);
- };
+ lock_guard<mutex> lock(this->_propertyMutex);
+ this->_connectionHandles[uri] = connection->get_handle();
+ this->_client.connect(connection);
+};
- tuple<string, bool> send(string message, string uri, function<void(const string&)> messageHandler) override
- {
- auto successes = this->send(message, uri);
- this->receive(uri, messageHandler);
- return successes;
- };
+bool nostr::client::WebsocketppClient::isConnected(string uri)
+{
+ lock_guard<mutex> lock(this->_propertyMutex);
+ return this->_connectionHandles.find(uri) != this->_connectionHandles.end();
+};
- void receive(string uri, function<void(const string&)> messageHandler) override
- {
- lock_guard<mutex> lock(this->_propertyMutex);
- auto connectionHandle = this->_connectionHandles[uri];
- auto connection = this->_client.get_con_from_hdl(connectionHandle);
+tuple<string, bool> nostr::client::WebsocketppClient::send(string message, string uri)
+{
+ error_code error;
- connection->set_message_handler([messageHandler](
- websocketpp::connection_hdl connectionHandle,
- websocketpp_client::message_ptr message)
- {
- messageHandler(message->get_payload());
- });
- };
+ // Make sure the connection isn't closed from under us.
+ lock_guard<mutex> lock(this->_propertyMutex);
+ this->_client.send(
+ this->_connectionHandles[uri],
+ message,
+ websocketpp::frame::opcode::text,
+ error);
- void closeConnection(string uri) override
+ if (error.value() == -1)
{
- lock_guard<mutex> lock(this->_propertyMutex);
+ return make_tuple(uri, false);
+ }
- websocketpp::connection_hdl handle = this->_connectionHandles[uri];
- this->_client.close(
- handle,
- websocketpp::close::status::going_away,
- "_client requested close.");
-
- this->_connectionHandles.erase(uri);
- };
+ return make_tuple(uri, true);
+};
-private:
- typedef websocketpp::client<websocketpp::config::asio_client> websocketpp_client;
- typedef unordered_map<string, websocketpp::connection_hdl>::iterator connection_hdl_iterator;
+tuple<string, bool> nostr::client::WebsocketppClient::send(
+ string message,
+ string uri,
+ function<void(const string&)> messageHandler)
+{
+ auto successes = this->send(message, uri);
+ this->receive(uri, messageHandler);
+ return successes;
+};
- websocketpp_client _client;
- unordered_map<string, websocketpp::connection_hdl> _connectionHandles;
- mutex _propertyMutex;
+void nostr::client::WebsocketppClient::receive(
+ string uri,
+ function<void(const string&)> messageHandler)
+{
+ lock_guard<mutex> lock(this->_propertyMutex);
+ auto connectionHandle = this->_connectionHandles[uri];
+ auto connection = this->_client.get_con_from_hdl(connectionHandle);
- void onMessage(websocketpp::connection_hdl handle, websocketpp_client::message_ptr message)
+ connection->set_message_handler([messageHandler](
+ websocketpp::connection_hdl connectionHandle,
+ websocketpp_client::message_ptr message)
{
- };
+ messageHandler(message->get_payload());
+ });
+};
+
+void nostr::client::WebsocketppClient::closeConnection(string uri)
+{
+ lock_guard<mutex> lock(this->_propertyMutex);
+
+ websocketpp::connection_hdl handle = this->_connectionHandles[uri];
+ this->_client.close(
+ handle,
+ websocketpp::close::status::going_away,
+ "_client requested close.");
+
+ this->_connectionHandles.erase(uri);
};
-} // namespace client
-} // namespace nostr