-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy paththreads.cpp
More file actions
76 lines (59 loc) · 2.45 KB
/
Copy paththreads.cpp
File metadata and controls
76 lines (59 loc) · 2.45 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include "WebSocketClient.h" // Include the thread-safe client class
int main() {
WebSocketClient client;
client.setUrl("ws://192.168.0.24:3001");
client.setMessageCallback([](const std::string& message) {
std::cout << "Received: " << message << std::endl;
});
int count= 0;
client.setBinaryCallback([&count](const void* data, size_t length) {
count++;
const uint8_t* bytes = static_cast<const uint8_t*>(data);
std::cout << "Received binary data (" << length << " bytes): " << " Count: " << count;
// Print first 16 bytes as hex
size_t display_bytes = std::min(length, size_t(16));
for (size_t i = 0; i < display_bytes; i++) {
std::cout << std::hex << static_cast<int>(bytes[i]) << " ";
}
std::cout << std::dec << std::endl;
});
client.setErrorCallback([&client](int error_code, const std::string& error_message) {
std::cout << "Error Callback in Main App: Calling disconnect" << std::endl;
client.disconnect();
});
// Connect to server
client.connect();
// Send a message from main thread
client.sendMessage("Hello from main thread!");
// Create another thread that sends messages - to demonstrate thread safety
std::thread message_thread([&client]() {
for (int i = 0; i < 5; i++) {
std::string msg = "Message from second thread: " + std::to_string(i);
client.sendMessage(msg);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if(i==4) {
std::vector<uint8_t> binary_data = {
0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0xFC
};
client.sendBinary(binary_data.data(), binary_data.size());
client.sendMessage("This is the final message from thread");
}
}
});
// Send binary data explicitly
std::vector<uint8_t> binary = {0x01, 0x02, 0x03};
client.sendBinary(binary.data(), binary.size());
// Wait for the message thread to complete
message_thread.join();
client.sendMessage("This is the final message from main thread!");
// Keep the program running to receive responses
std::cout << "Press Enter to quit..." << std::endl;
std::cin.get();
// Disconnect when done
client.disconnect();
return 0;
}