|
1 | 1 | #include "App.h" |
2 | | -#include <thread> |
3 | | -#include <algorithm> |
4 | | -#include <mutex> |
5 | | - |
6 | | -/* Note that SSL is disabled unless you build with WITH_OPENSSL=1 */ |
7 | | -const int SSL = 1; |
8 | | -std::mutex stdoutMutex; |
| 2 | +#include "LocalCluster.h" |
9 | 3 |
|
10 | 4 | int main() { |
11 | | - /* Overly simple hello world app, using multiple threads */ |
12 | | - std::vector<std::thread *> threads(std::thread::hardware_concurrency()); |
13 | | - |
14 | | - std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread */*t*/) { |
15 | | - return new std::thread([]() { |
16 | | - |
17 | | - uWS::SSLApp({ |
18 | | - .key_file_name = "misc/key.pem", |
19 | | - .cert_file_name = "misc/cert.pem", |
20 | | - .passphrase = "1234" |
21 | | - }).get("/*", [](auto *res, auto * /*req*/) { |
22 | | - res->end("Hello world!"); |
23 | | - }).listen(3000, [](auto *listen_socket) { |
24 | | - stdoutMutex.lock(); |
25 | | - if (listen_socket) { |
26 | | - /* Note that us_listen_socket_t is castable to us_socket_t */ |
27 | | - std::cout << "Thread " << std::this_thread::get_id() << " listening on port " << us_socket_local_port(SSL, (struct us_socket_t *) listen_socket) << std::endl; |
28 | | - } else { |
29 | | - std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 3000" << std::endl; |
30 | | - } |
31 | | - stdoutMutex.unlock(); |
32 | | - }).run(); |
33 | | - |
| 5 | + /* Note that SSL is disabled unless you build with WITH_OPENSSL=1 */ |
| 6 | + uWS::LocalCluster({ |
| 7 | + .key_file_name = "misc/key.pem", |
| 8 | + .cert_file_name = "misc/cert.pem", |
| 9 | + .passphrase = "1234" |
| 10 | + }, |
| 11 | + [](uWS::SSLApp &app) { |
| 12 | + /* Here this App instance is defined */ |
| 13 | + app.get("/*", [](auto *res, auto * /*req*/) { |
| 14 | + res->end("Hello world!"); |
| 15 | + }).listen(3000, [](auto *listen_socket) { |
| 16 | + if (listen_socket) { |
| 17 | + /* Note that us_listen_socket_t is castable to us_socket_t */ |
| 18 | + std::cout << "Thread " << std::this_thread::get_id() << " listening on port " << us_socket_local_port(true, (struct us_socket_t *) listen_socket) << std::endl; |
| 19 | + } else { |
| 20 | + std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 3000" << std::endl; |
| 21 | + } |
34 | 22 | }); |
35 | 23 | }); |
36 | | - |
37 | | - std::for_each(threads.begin(), threads.end(), [](std::thread *t) { |
38 | | - t->join(); |
39 | | - }); |
40 | 24 | } |
0 commit comments