aboutsummaryrefslogtreecommitdiff
path: root/include/client/websocketpp_client.hpp
blob: 27a99b3fe9529ba9bb29bc670f5ac3e1e8477520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#include <mutex>
#include <unordered_map>
#include <tuple>

#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>

#include "client/web_socket_client.hpp"

namespace nostr
{
namespace client
{
/**
 * @brief An implementation of the `IWebSocketClient` interface that uses the WebSocket++ library.
 */
class WebsocketppClient : public IWebSocketClient
{
public:
    void start() override;

    void stop() override;

    void openConnection(std::string uri) override;

    bool isConnected(std::string uri) override;

    std::tuple<std::string, bool> send(std::string message, std::string uri) override;

    std::tuple<std::string, bool> send(
        std::string message,
        std::string uri,
        std::function<void(const std::string&)> messageHandler) override;

    void receive(std::string uri, std::function<void(const std::string&)> messageHandler) override;

    void closeConnection(std::string uri) override;

private:
    typedef websocketpp::client<websocketpp::config::asio_client> websocketpp_client;
    typedef std::unordered_map<std::string, websocketpp::connection_hdl>::iterator connection_hdl_iterator;

    websocketpp_client _client;
    std::unordered_map<std::string, websocketpp::connection_hdl> _connectionHandles;
    std::mutex _propertyMutex;
};
} // namespace client
} // namespace nostr