-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (36 loc) · 1.44 KB
/
main.cpp
File metadata and controls
40 lines (36 loc) · 1.44 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
#include <uWS/uWS.h>
#include <iostream>
#include <thread>
#include <algorithm>
#define LISTEN_PORT 3000
int main(int argc, char* argv[]) {
(void) argc; (void) argv;
uS::TLS::Context tls_context = uS::TLS::createContext("./cert.pem", "./pkey.pem");
if (!tls_context) {
std::cerr << "Cannot create tls context" << std::endl;
return EXIT_FAILURE;
}
std::vector<std::thread *> threads(std::thread::hardware_concurrency());
std::transform(threads.begin(), threads.end(), threads.begin(), [&](std::thread *t) {
(void) t;
return new std::thread([&]() -> int {
uWS::Hub h;
uWS::Group<uWS::SERVER> *ssl_group = h.createGroup<uWS::SERVER>(uWS::PERMESSAGE_DEFLATE);
ssl_group->onMessage([](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
std::cout << "Thread ID: " << std::this_thread::get_id() << ", Addr: " << ws->getAddress().address << ", Port: " << ws->getAddress().port << ": ";
std::cout.write(message, length);
std::cout << std::endl;
ws->send(message, length, opCode);
});
if (!h.listen(LISTEN_PORT, tls_context, uS::ListenOptions::REUSE_PORT, ssl_group)) {
std::cerr << "Failed to listen to Websocket Secure on port " << LISTEN_PORT << std::endl;
return EXIT_FAILURE;
}
h.run();
return EXIT_SUCCESS;
});
});
std::for_each(threads.begin(), threads.end(), [](std::thread *t) {
t->join();
});
}