aboutsummaryrefslogtreecommitdiff
path: root/src/client/websocketpp_client.cpp
blob: e4f6af6c327b3481feddb942f9916177742e487a (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <mutex>

#include "client/websocketpp_client.hpp"

using namespace std;

void nostr::client::WebsocketppClient::start()
{ 
    this->_client.init_asio();
    this->_client.start_perpetual();
};

void nostr::client::WebsocketppClient::stop()
{
    this->_client.stop_perpetual();
    this->_client.stop();
};

void nostr::client::WebsocketppClient::openConnection(string uri)
{
    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();
    }

    // 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);
        }
    });

    lock_guard<mutex> lock(this->_propertyMutex);
    this->_connectionHandles[uri] = connection->get_handle();
    this->_client.connect(connection);
};

bool nostr::client::WebsocketppClient::isConnected(string uri)
{
    lock_guard<mutex> lock(this->_propertyMutex);
    return this->_connectionHandles.find(uri) != this->_connectionHandles.end();
};

tuple<string, bool> nostr::client::WebsocketppClient::send(string message, string uri)
{
    error_code error;

    // 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);

    if (error.value() == -1)    
    {
        return make_tuple(uri, false);
    }

    return make_tuple(uri, true);
};

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;
};

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);

    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);
};