-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.cpp
More file actions
69 lines (57 loc) · 1.68 KB
/
Copy pathconnection.cpp
File metadata and controls
69 lines (57 loc) · 1.68 KB
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
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "kinopio/kinopio.hpp"
namespace {
std::vector<std::string> splitServers(const std::string& value) {
std::vector<std::string> servers;
std::size_t start = 0;
while (start <= value.size()) {
const auto comma = value.find(',', start);
const auto length = comma == std::string::npos ? value.size() - start : comma - start;
const auto token = value.substr(start, length);
if (!token.empty()) {
servers.push_back(token);
}
if (comma == std::string::npos) {
break;
}
start = comma + 1;
}
return servers;
}
std::vector<std::string> resolveServers() {
if (const char* value = std::getenv("KINOPIO_NATS_URLS")) {
const auto servers = splitServers(value);
if (!servers.empty()) {
return servers;
}
}
if (const char* value = std::getenv("KINOPIO_NATS_URL")) {
return {value};
}
return {"nats://demo.nats.io:4222"};
}
} // namespace
int main() {
kinopio::KinopioOptions options;
options.servers = resolveServers();
options.serverSelectionMode = kinopio::ServerSelectionMode::latency;
kinopio::KinopioHub hub(options);
hub.connected(std::chrono::seconds(5));
std::cout << "Configured servers:";
for (const auto& server : options.servers) {
std::cout << ' ' << server;
}
std::cout << '\n';
std::cout << "Is connected: " << (hub.isConnected() ? "yes" : "no") << '\n';
// Keep connection alive for 3 seconds like JS example
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << "Disconnecting...\n";
hub.dispose();
std::cout << "Disconnected gracefully\n";
return 0;
}