-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
110 lines (88 loc) · 3.01 KB
/
client.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
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
108
109
110
#include "loon/client.h"
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <openssl/rand.h>
#ifdef USES_QT
#include <QCoreApplication>
#include <QTimer>
#endif
using namespace std::chrono_literals;
int run(loon::Client* client);
int main(int argc, char** argv)
{
#ifdef USES_QT
QCoreApplication app(argc, argv);
#endif
loon::client_log_level(loon::LogLevel::Debug);
loon::websocket_log_level(loon::LogLevel::Warning);
std::string address = "ws://127.0.0.1:8071/ws";
loon::ClientOptions options;
// options.websocket.basic_authorization =
// "loon-client:qadjB4GeRyUSEjbj6ZFWwOiDtjLq";
// options.websocket.ca_certificate_path =
// "W:\\dev\\projects\\loon\\deployments\\cert.pem";
options.websocket.connect_timeout = 5000ms;
#ifndef USES_QT
// QT does not need a ping interval.
options.websocket.ping_interval = 20000ms;
#endif
// options.websocket.reconnect_delay = 1000ms;
// options.websocket.max_reconnect_delay = 30000ms;
options.no_content_request_limit = std::make_pair(16, 1s);
loon::Client client(address, options);
client.start();
#ifdef USES_QT
QTimer::singleShot(0, [&]() {
app.exit(run(&client));
});
app.exec();
#else
run(&client);
#endif
}
int run(loon::Client* client)
{
// constexpr size_t path_size = 4;
// unsigned char path[path_size];
// RAND_bytes(path, path_size);
// std::cout << std::string(reinterpret_cast<const char*>(path), path_size)
// << std::endl;
const uint32_t cache_duration = 30;
// std::string address = "wss://echo.websocket.org";
// options.min_cache_duration = (cache_duration * 4) / 5;
// options.max_requests_per_second = 1.0f;
client->wait_until_ready(20s);
std::ostringstream oss;
oss << "<h1>It works!</h1><br>";
// for (size_t i = 0; i < 2000; i++) { // 1000
// oss << "<p>This is such an interesting thing to read!</p>";
// }
// std::string content = "<h1>It works!";
std::string content = oss.str();
std::string content_type = "text/plain";
std::vector<char> content_data(content.begin(), content.end());
auto content_source =
std::make_shared<loon::BufferContentSource>(content_data, content_type);
loon::ContentInfo content_info;
content_info.path = "index.html";
content_info.max_cache_duration = cache_duration;
auto handle = client->register_content(content_source, content_info);
handle->on_unregistered([] {
std::cout << "unregistered\n";
});
std::cout << "URL: " << handle->url() << std::endl;
std::this_thread::sleep_for(30s);
std::cout << "Unregistering content" << std::endl;
client->unregister_content(handle);
std::this_thread::sleep_for(1s);
std::cout << "Stopping client" << std::endl;
client->stop();
std::this_thread::sleep_for(1s);
std::cout << "Exiting" << std::endl;
// for (size_t i = 0; i < 32000; i++) {
// content += std::string("\n\n") + "okay";
// }
return 0;
}