-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
43 lines (32 loc) · 1.03 KB
/
Copy pathmain.cpp
File metadata and controls
43 lines (32 loc) · 1.03 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
#include <iostream>
#include <crow.h>
void onOpen(crow::websocket::connection &conn) {
std::cout << "onOpen" << std::endl;
}
void onClose(crow::websocket::connection &conn, const std::string &reason, uint16_t code) {
std::cout << "onClose: " << reason << std::endl;
}
void onMessage(crow::websocket::connection &conn, const std::string &data, bool is_binary) {
std::cout << "onMessage: " << data << std::endl;
for (int i = 0; i < 2; i++) {
conn.send_text(std::format("response {}", i));
}
}
int main() {
auto app = crow::Crow();
CROW_WEBSOCKET_ROUTE(app, "/")
.onopen(onOpen)
.onclose(onClose)
.onmessage(onMessage);
const std::future<void> serverShutdownFeature = app.bindaddr("127.0.0.1").port(1337).run_async();
app.wait_for_server_start();
std::cout << "Server started on port: " << app.port() << std::endl;
while (true) {
if (getchar() == 'q') {
break;
}
}
app.stop();
serverShutdownFeature.wait();
return 0;
}