From a437d34b29d2a65113f3f67ffa1a6c3391b7e836 Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Mon, 18 Mar 2024 20:51:22 -0500 Subject: Implement receive method in WebSocket client --- src/client/websocketpp_client.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/client/websocketpp_client.cpp b/src/client/websocketpp_client.cpp index 1386e1a..5199343 100644 --- a/src/client/websocketpp_client.cpp +++ b/src/client/websocketpp_client.cpp @@ -1,9 +1,12 @@ +#include #include #include #include "web_socket_client.hpp" +using nlohmann::json; using std::error_code; +using std::function; using std::lock_guard; using std::make_tuple; using std::mutex; @@ -83,6 +86,29 @@ public: return make_tuple(uri, true); }; + void receive(string uri, function messageHandler) override + { + this->_client.set_message_handler([this, 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. + }); + }; + void closeConnection(string uri) override { lock_guard lock(this->_propertyMutex); @@ -103,5 +129,9 @@ private: websocketpp_client _client; unordered_map _connectionHandles; mutex _propertyMutex; + + void onMessage(websocketpp::connection_hdl handle, websocketpp_client::message_ptr message) + { + }; }; } // namespace client -- cgit