-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
37 lines (28 loc) · 1.22 KB
/
main.cpp
File metadata and controls
37 lines (28 loc) · 1.22 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
#include "custom_websocket_client.hpp"
#include <iostream>
#include <string>
int main() {
custom_websocket_client client;
std::string server_url = "ws://localhost:8080"; // wss:// (WebSocket Secure) protocol is also supported.
client.set_url(server_url);
client.start(); // Start the WebSocket client
std::cout << "--- Enter message to send (type 'exit' to quit) ---" << std::endl;
std::string user_input;
user_input = "hello world"; // Initial test message
while (true) {
// if (!std::getline(std::cin, user_input)) break;
// if (user_input == "exit") break;
if (!user_input.empty()) {
if (client.isReady()) {
std::cout << "[SEND] " << user_input << std::endl;
client.send_message(user_input);
// Send messages in a thread-safe manner. Can be called from external threads.
} else {
std::cerr << "[Warning] Connection is not open. Message will be sent when connection is established." << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Brief wait to prevent CPU overuse
}
client.stop(); // Stop the WebSocket client
return 0;
}