-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
36 lines (32 loc) · 1.18 KB
/
main.cpp
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
#include <libwebsockets.h>
#include "src/libs/callback.h"
#include "src/libs/websocket_client.h"
class websocket : public websocket_client, public client_callback_t {
bool run_ = true;
public:
websocket() : websocket_client(this, "ws-feed.pro.coinbase.com") {}
[[nodiscard]] bool working() const noexcept { return run_; }
void on_connected() override {
lwsl_user("client connected\n");
std::string msg = R"({ "type": "subscribe", "product_ids": [ "ETH-USD", "ETH-EUR" ], "channels": [ "level2", "heartbeat", { "name": "ticker", "product_ids": [ "ETH-BTC", "ETH-USD" ] } ] })";
send(msg.data(), msg.size());
}
void on_disconnected() override {
lwsl_user("client disconnected\n");
}
void on_error(const char* msg, size_t len) override {
lwsl_user("client error\n");
}
void on_data(const char* data, size_t len, size_t remaining) override {
std::string msg(data, len);
lwsl_user("data from server: %s\n", msg.c_str());
}
};
int main() {
lws_set_log_level(LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, nullptr);
websocket client;
client.connect();
while (client.working()) {
}
return 0;
}