Kurlyk
Loading...
Searching...
No Matches
websocket_client_lifecycle_test.cpp
Go to the documentation of this file.
1#include <kurlyk.hpp>
2#include <thread>
3#include <chrono>
4
6 for (int i = 0; i < n; ++i) {
7 KURLYK_PRINT << "Iteration " << i + 1 << " of " << n << std::endl;
8
9 // First client: connects and disconnects in quick succession, then exits scope
10 {
11 kurlyk::WebSocketClient client("wss://echo-websocket.fly.dev/");
12 client.on_event([](std::unique_ptr<kurlyk::WebSocketEventData> event) {
13 switch (event->event_type) {
15 KURLYK_PRINT << "Client 1: Connection opened" << std::endl;
16 break;
18 KURLYK_PRINT << "Client 1: Message received: " << event->message << std::endl;
19 break;
21 KURLYK_PRINT << "Client 1: Connection closed: " << event->message
22 << "; Status code: " << event->status_code << std::endl;
23 break;
25 KURLYK_PRINT << "Client 1: Error: " << event->error_code.message() << std::endl;
26 break;
27 };
28 });
29 for (int j = 0; j < 100; ++j) {
30 client.connect();
31 client.disconnect();
32 }
33 KURLYK_PRINT << "Client 1: Connecting..." << std::endl;
34 client.connect();
35 } // Client 1 exits scope here
36
37 // Second client: connects, stays connected for 60 seconds, then exits scope
38 {
39 kurlyk::WebSocketClient client("wss://echo-websocket.fly.dev/");
40 const long rate_limit_id = client.add_rate_limit_rps(2);
41 KURLYK_PRINT << "Assigned rate limit ID: " << rate_limit_id << std::endl;
42 client.on_event([rate_limit_id](std::unique_ptr<kurlyk::WebSocketEventData> event) {
43 static int counter = 0;
44 switch (event->event_type) {
46 KURLYK_PRINT << "Client 2: Connection opened" << std::endl;
47 // Send a message
48 event->sender->send_message("Hello, WebSocket! Counter: " + std::to_string(counter++), rate_limit_id);
49 break;
51 KURLYK_PRINT << "Client 2: Message received: " << event->message << std::endl;
52 // Send a response
53 event->sender->send_message("Hello again! Counter: " + std::to_string(counter++), rate_limit_id);
54 break;
56 KURLYK_PRINT << "Client 2: Connection closed: " << event->message
57 << "; Status code: " << event->status_code << std::endl;
58 break;
60 KURLYK_PRINT << "Client 2: Error: " << event->error_code.message() << std::endl;
61 break;
62 };
63 });
64 for (int j = 0; j < 100; ++j) {
65 client.connect();
66 client.disconnect();
67 }
68 KURLYK_PRINT << "Client 2: Connecting..." << std::endl;
69 client.connect();
70 std::this_thread::sleep_for(std::chrono::seconds(60));
71 KURLYK_PRINT << "Client 2: Disconnecting..." << std::endl;
72 client.disconnect_and_wait();
73 } // Client 2 exits scope after 60 seconds
74 }
75}
76
77int main() {
78 int repeat_count = 5; // Number of times to repeat the connect/disconnect cycle
79 test_connect_disconnect(repeat_count);
80 return 0;
81}
82
Provides an interface for managing WebSocket connections, including configuration,...
void on_event(std::function< void(std::unique_ptr< WebSocketEventData >)> callback)
Sets a callback for WebSocket events.
bool disconnect_and_wait()
Disconnects from the WebSocket server, blocking until the disconnection completes.
long add_rate_limit_rps(long requests_per_second)
Adds a rate limit based on Requests Per Second (RPS).
std::future< bool > connect()
Asynchronously connects to the WebSocket server.
std::future< bool > disconnect()
Asynchronously disconnects from the WebSocket server.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
@ WS_ERROR
Error occurred.
Definition enums.hpp:35
@ WS_MESSAGE
Message received.
Definition enums.hpp:33
@ WS_CLOSE
Connection closed.
Definition enums.hpp:34
@ WS_OPEN
Connection established.
Definition enums.hpp:32
#define KURLYK_PRINT
void test_connect_disconnect(int n)