-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
64 lines (58 loc) · 1.9 KB
/
Copy pathmain.cpp
File metadata and controls
64 lines (58 loc) · 1.9 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
#include <iostream>
#include <thread>
#include <wsio.h>
int main()
{
wscurl::wsio_t ws;
ws
// lambda callbacks
.on_event(
[](wscurl::event_t ev, const std::string &info)
{
switch (ev) {
case wscurl::event_t::CONNECT_EV:
std::cerr << "I -> connected info: " << info << std::endl;
break;
case wscurl::event_t::DISCONNECT_EV:
std::cerr << "I -> disconnected reason: " << info << std::endl;
break;
case wscurl::event_t::ERROR_EV:
std::cerr << "E -> " << info << std::endl;
break;
}
}
)
.on_message_binary(
[](const std::vector<uint8_t> &data)
{
std::cerr << "D -> " << data.size() << " bytes, chars: " << std::string((const char*)data.data(), data.size()) << std::endl;
}
)
.on_header(
[](const std::string &hdr_name, const std::string &hdr_val)
{}
);
struct S { // struct with callbacks
void message_cb(const std::string &msg) {
std::cerr << "S:message_cb -> " << msg << std::endl;
}
} struct_callback;
ws.on_message_text<S, &S::message_cb>(&struct_callback);
// sync mode
if(ws.start("wss://ws.vi-server.org/mirror", "", false)) {
ws.write("Hello world!");
ws.read();
ws.write(std::vector<uint8_t>{'B','y','e',' ','w','o','r','l','d','!'});
ws.read();
}
ws.stop();
// async mode
if(ws.start("wss://ws.vi-server.org/mirror")) {
ws.write("Hello world!");
std::this_thread::sleep_for(std::chrono::seconds(1));
ws.write("Bye world!");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
ws.stop();
return 0;
}