diff --git a/main.cpp b/main.cpp index 04b97f3..a957edd 100644 --- a/main.cpp +++ b/main.cpp @@ -10,18 +10,18 @@ #include #include +#include #include #include -#include using namespace std; using namespace std::chrono_literals; using easywsclient::WebSocket; using json = nlohmann::json; -atomic run_loop(true); // Run thread loops +atomic run_loop(true); // Run thread loops atomic close_ws(false); // control websocket -mutex guard; // to protect ws +mutex guard; // to protect ws // For console exit keywords unordered_set exit_key({ "q", "quit", "exit" }); @@ -32,16 +32,44 @@ unordered_set help_key({ "h", "help" }); // For reconnect ws unordered_set reconnect_key({ "r", "reset", "reconnect", "restart" }); - void websocket(string uri) { - while(run_loop) { + while (run_loop) { WebSocket::pointer ws = WebSocket::from_url(uri); if (ws == NULL) { //console.error("WebSocket not connected: " + uri); - console.info("Trying to reconnect in 5 sec.."); - this_thread::sleep_for(5000ms); + //console.info("Trying to reconnect in 5 sec.."); + + // Animation in console + // more info: https://stackoverflow.com/questions/8486181/how-to-make-a-loading-animation-in-console-application-written-in-c + cout << console.get( + "Trying to reconnect in:", + { console.light_magenta, console.underline }) << " "; + int wait_for = 5; + for (int i=0; i> keyword; if (exit_key.find(keyword) != exit_key.end()) { //{ - //const lock_guard lock(guard); - //ws->close(); + //const lock_guard lock(guard); + //ws->close(); //} console.debug("Exit"); close_ws = true; @@ -112,7 +140,6 @@ int main(int argc, char* argv[]) uri = "ws://local"; } - thread keyListener(keybord); thread wsListener(websocket, uri); @@ -124,6 +151,5 @@ int main(int argc, char* argv[]) console.warn(msg); } - return 0; }