-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
49 lines (42 loc) · 1.39 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
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <fmt/core.h>
#include <thread>
#include "websocket/connect_options.h"
#include "websocket/connection.h"
boost::asio::awaitable<void> streaming() {
std::string host = "streaming.saxobank.com";
std::string target =
"/sim/openapi/streamingws/connect?contextId=some-context-id";
std::string access_token = "invalid-token";
websocket::connect_options opts;
opts.pingpong_timeout = std::chrono::seconds{0};
opts.headers.set(boost::beast::http::field::host, host);
opts.headers.set(boost::beast::http::field::authorization,
"Bearer " + access_token);
// never stop...
while (true) {
auto ws = co_await websocket::connect(host, "443", target, opts);
for (;;) {
auto ev = co_await ws.consume();
if (ev.is_error()) {
if (ev.error() == boost::beast::websocket::error::closed) {
fmt::print("connection closed: {}\n", ws.reason());
co_return;
} else {
fmt::print("connection error: {}, reconnecting\n",
ev.error().message());
break;
}
} else {
fmt::print("handle message: {}\n", ev.message().text());
}
}
std::this_thread::sleep_for(std::chrono::seconds{5});
}
}
int main(int argc, char **argv) {
boost::asio::io_context ioc;
boost::asio::co_spawn(
ioc, [] { return streaming(); }, boost::asio::detached);
ioc.run();
return 0;
}