How could I exit from the run()
call in the official example? For example after receiving a signal.
uWS::SSLApp({
/* There are tons of SSL options */
.cert_file_name = "cert.pem",
.key_file_name = "key.pem"
}).onGet("/", [](auto *res, auto *req) {
/* Respond with the web app on default route */
res->writeStatus("200 OK")
->writeHeader("Content-Type", "text/html; charset=utf-8")
->end(indexHtmlBuffer);
}).onWebSocket<UserData>("/ws/chat", [&](auto *ws, auto *req) {
/* Subscribe to topic /chat */
ws->subscribe("chat");
}).onMessage([&](auto *ws, auto message, auto opCode) {
/* Parse incoming message according to some protocol & publish it */
if (seemsReasonable(message)) {
ws->publish("chat", message);
} else {
ws->close();
}
}).onClose([&](auto *ws, int code, auto message) {
/* Remove websocket from this topic */
ws->unsubscribe("chat");
}).listen("localhost", 3000, 0).run();
In a documentation, there is written following:
Because the App itself is under RAII control, once the blocking .run call returns and the App goes out of scope, all memory will gracefully be deleted.
So it means you have to release every source inside of the function. So e.g. this:
And after calling testThread, the server should exit (if no client is connected, otherwise, you should also disconnect connected clients (sockets)) and continue after run() line. After disconnect the client, my output is following:
Listening on port 9001
Client disconnect!
Client disconnect!
Client disconnect!
Client disconnect!
Client disconnect!
Client disconnect!
Shutdown!