diff options
-rw-r--r-- | include/client/web_socket_client.hpp | 6 | ||||
-rw-r--r-- | include/nostr.hpp | 7 | ||||
-rw-r--r-- | src/client/websocketpp_client.cpp | 25 | ||||
-rw-r--r-- | src/nostr_service.cpp | 27 | ||||
-rw-r--r-- | test/nostr_service_test.cpp | 2 |
5 files changed, 39 insertions, 28 deletions
diff --git a/include/client/web_socket_client.hpp b/include/client/web_socket_client.hpp index f676e59..3ef2b86 100644 --- a/include/client/web_socket_client.hpp +++ b/include/client/web_socket_client.hpp @@ -45,10 +45,10 @@ public: /** * @brief Sets up a message handler for the given server. * @param uri The URI of the server to which the message handler should be attached. - * @param messageHandler A callable object that will be invoked with the subscription ID and - * the message contents when the client receives a message from the server. + * @param messageHandler A callable object that will be invoked with the payload the client + * receives from the server. */ - virtual void receive(std::string uri, std::function<void(const std::string&, const std::string&)> messageHandler) = 0; + virtual void receive(std::string uri, std::function<void(const std::string&)> messageHandler) = 0; /** * @brief Closes the connection to the given server. diff --git a/include/nostr.hpp b/include/nostr.hpp index 3e60d7b..2b04862 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -170,7 +170,7 @@ public: * events returned from the relay for the given filters. The service will not store the * events, and they will not be accessible via `getNewEvents`. */ - std::string queryRelays(Filters filters, std::function<void(std::string, Event)> responseHandler); + std::string queryRelays(Filters filters, std::function<void(const std::string&, Event)> responseHandler); /** * @brief Get any new events received since the last call to this method, across all @@ -285,6 +285,11 @@ private: bool hasSubscription(std::string relay, std::string subscriptionId); /** + * @brief Parses messages received from the relay and invokes the appropriate message handler. + */ + void onMessage(std::string message, std::function<void(const std::string&, Event)> eventHandler); + + /** * @brief A default message handler for events returned from relay queries. * @param subscriptionId The ID of the subscription for which the event was received. * @param event The event received from the relay. diff --git a/src/client/websocketpp_client.cpp b/src/client/websocketpp_client.cpp index 5199343..981d4ec 100644 --- a/src/client/websocketpp_client.cpp +++ b/src/client/websocketpp_client.cpp @@ -1,10 +1,8 @@ -#include <nlohmann/json.hpp> #include <websocketpp/client.hpp> #include <websocketpp/config/asio_client.hpp> #include "web_socket_client.hpp" -using nlohmann::json; using std::error_code; using std::function; using std::lock_guard; @@ -86,26 +84,17 @@ public: return make_tuple(uri, true); }; - void receive(string uri, function<void(const string&, const string&)> messageHandler) override + void receive(string uri, function<void(const string&)> messageHandler) override { - this->_client.set_message_handler([this, messageHandler]( + lock_guard<mutex> lock(this->_propertyMutex); + auto connectionHandle = this->_connectionHandles[uri]; + auto connection = this->_client.get_con_from_hdl(connectionHandle); + + connection->set_message_handler([messageHandler]( websocketpp::connection_hdl connectionHandle, websocketpp_client::message_ptr message) { - json jarr = json::array(); - string payload = message->get_payload(); - - jarr.parse(payload); - string messageType = jarr[0]; - - if (messageType == "EVENT") - { - string subscriptionId = jarr[1]; - string messageContents = jarr[2].dump(); - messageHandler(subscriptionId, messageContents); - }; - - // TODO: Add support for other message types. + messageHandler(message->get_payload()); }); }; diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 7efc11e..ac63f23 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -164,7 +164,7 @@ string NostrService::queryRelays(Filters filters) }); }; -string NostrService::queryRelays(Filters filters, function<void(string, Event)> responseHandler) +string NostrService::queryRelays(Filters filters, function<void(const string&, Event)> responseHandler) { RelayList successfulRelays; RelayList failedRelays; @@ -182,10 +182,8 @@ string NostrService::queryRelays(Filters filters, function<void(string, Event)> }); requestFutures.push_back(move(requestFuture)); - this->_client->receive(relay, [responseHandler](string subscriptionId, string message) { - Event event; - event.deserialize(message); - responseHandler(subscriptionId, event); + this->_client->receive(relay, [this, responseHandler](string payload) { + this->onMessage(payload, responseHandler); }); } @@ -460,6 +458,25 @@ bool NostrService::hasSubscription(string relay, string subscriptionId) return false; }; +void NostrService::onMessage(string message, function<void(const string&, Event)> eventHandler) +{ + json jarr = json::array(); + jarr = json::parse(message); + + string messageType = jarr[0]; + + if (messageType == "EVENT") + { + string subscriptionId = jarr[1]; + string serializedEvent = jarr[2].dump(); + Event event; + event.deserialize(message); + eventHandler(subscriptionId, event); + } + + // Support other message types here, if necessary. +}; + void NostrService::onEvent(string subscriptionId, Event event) { lock_guard<mutex> lock(this->_propertyMutex); diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 70f4d9e..1679ac5 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -28,7 +28,7 @@ public: MOCK_METHOD(void, openConnection, (string uri), (override)); MOCK_METHOD(bool, isConnected, (string uri), (override)); MOCK_METHOD((tuple<string, bool>), send, (string message, string uri), (override)); - MOCK_METHOD(void, receive, (string uri, function<void(const string&, const string&)> messageHandler), (override)); + MOCK_METHOD(void, receive, (string uri, function<void(const string&)> messageHandler), (override)); MOCK_METHOD(void, closeConnection, (string uri), (override)); }; |