Skip to content

Commit

Permalink
Send JSON message to WebSocket server added.
Browse files Browse the repository at this point in the history
  • Loading branch information
memoryInject committed Mar 23, 2022
1 parent b21cb59 commit 1341842
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using json = nlohmann::json;

atomic<bool> run_loop(true); // Run thread loops
atomic<bool> close_ws(false); // control websocket
atomic<bool> send_ws(false); // send message to WebSocket
atomic<bool> send_ws(false); // send message to WebSocket
mutex guard; // to protect ws

// For console exit keywords
Expand All @@ -36,49 +36,34 @@ unordered_set<string> reconnect_key({ "r", "reset", "reconnect", "restart" });
// For sending messages
unordered_set<string> message_key({ "res", "send", "response" });


// Write message to
string message;
// Message for send
string send_msg;

void websocket(string uri)
{
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..");

// 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 }) << " ";
"Trying to reconnect in:",
{ console.light_magenta, console.underline })
<< " ";
int wait_for = 5;
for (int i=0; i<wait_for; i++) {
if (close_ws){
for (int i = 0; i < wait_for; i++) {
if (close_ws) {
break;
}

cout << "\b\b" + to_string(wait_for - i) + "s" << flush;
this_thread::sleep_for(1000ms);
}
if (close_ws == false){
cout << "\b\b0s" << endl;
if (!close_ws) {
cout << "\b\b0s" << endl;
}

//cout << "\b\b5s" << flush;
//this_thread::sleep_for(1000ms);
//cout << "\b\b4s" << flush;
//this_thread::sleep_for(1000ms);
//cout << "\b\b3s" << flush;
//this_thread::sleep_for(1000ms);
//cout << "\b\b2s" << flush;
//this_thread::sleep_for(1000ms);
//cout << "\b\b1s" << flush;
//this_thread::sleep_for(1000ms);
//cout << "\b\b0s" << endl;
//console.log("Reconnect: " + uri);
continue;
}

Expand All @@ -103,16 +88,15 @@ void websocket(string uri)
});

if (send_ws) {
ws->send(message);
ws->send(send_msg);
send_ws = false;
}


if (close_ws == true) {
if (close_ws) {
ws->close();
close_ws = false;
}
}
close_ws = false;
delete ws;
//run_loop = false;
}
Expand All @@ -124,10 +108,6 @@ void keybord()
string keyword;
cin >> keyword;
if (exit_key.find(keyword) != exit_key.end()) {
//{
//const lock_guard<mutex> lock(guard);
//ws->close();
//}
console.debug("Exit");
close_ws = true;
run_loop = false;
Expand All @@ -140,9 +120,30 @@ void keybord()

if (message_key.find(keyword) != message_key.end()) {
if (!send_ws) {
send_msg = "";
char c;

cout << "Message: ";
cin >> message;
// To get raw input from user
// ref: https://stackoverflow.com/questions/53797121/how-to-read-raw-input-in-cpp
cin.get(c); // To skip first '\n' when enter 'res'
while (cin.get(c)) {
if (c == '\n') {
break;
}
send_msg.push_back(c);
}
send_ws = true;
// Check if the message in JSON
try {
json j_complete = json::parse(send_msg);
console.info("\n<<< JSON Data:");
cout << setw(4) << j_complete << endl;
} catch (const exception& e) {
cout
<< console.get("\n<<< ", { console.light_blue })
<< send_msg << endl;
}
} else {
console.warn("WebSocket is busy...");
}
Expand Down

0 comments on commit 1341842

Please sign in to comment.