99 * that can be found in the License file.
1010 *
1111 * Vix.cpp
12- * @brief Minimal WebSocket client example for Vix.cpp
12+ * @brief Minimal WebSocket client example for Vix.cpp
1313 *
14- * This example demonstrates the simplest possible interactive WebSocket
15- * client built with the Vix.cpp WebSocket module. It connects to a server,
16- * listens for typed JSON messages, prints structured chat output, and allows
17- * the user to send messages through a basic terminal prompt.
14+ * This example demonstrates the simplest possible interactive WebSocket
15+ * client built with the Vix.cpp WebSocket module. It connects to a server,
16+ * listens for typed JSON messages, prints structured chat output, and allows
17+ * the user to send messages through a basic terminal prompt.
1818 *
19- * Core Features Demonstrated
20- * ---------------------------
21- * 1. Client Creation:
22- * The example creates a WebSocket client targeting localhost:9090 and
23- * automatically manages connection state through Vix.cpp abstractions.
19+ * Core Features Demonstrated
20+ * ---------------------------
21+ * 1. Client Creation:
22+ * The example creates a WebSocket client targeting localhost:9090 and
23+ * automatically manages connection state through Vix.cpp abstractions.
2424 *
25- * 2. Typed JSON Protocol Handling:
26- * Incoming frames are parsed using JsonMessage and routed based on their
27- * "type":
28- * • chat.system – server/system events
29- * • chat.message – regular chat messages
30- * • fallback – prints raw JSON
25+ * 2. Typed JSON Protocol Handling:
26+ * Incoming frames are parsed using JsonMessage and routed based on their
27+ * "type":
28+ * • chat.system - server/system events
29+ * • chat.message - regular chat messages
30+ * • fallback - prints raw JSON
3131 *
32- * 3. Auto-Reconnect:
33- * The client will automatically attempt to reconnect every 3 seconds
34- * if the connection is lost.
32+ * 3. Auto-Reconnect:
33+ * The client will automatically attempt to reconnect every 3 seconds
34+ * if the connection is lost.
3535 *
36- * 4. Heartbeat / Keep-Alive:
37- * A periodic ping is enabled to keep NAT/proxy connections alive.
36+ * 4. Heartbeat / Keep-Alive:
37+ * A periodic ping is enabled to keep NAT/proxy connections alive.
3838 *
39- * 5. Interactive Input Loop:
40- * The user enters a pseudonym and can then type messages in real time.
41- * Typing "/quit" closes the session gracefully.
39+ * 5. Interactive Input Loop:
40+ * The user enters a pseudonym and can then type messages in real time.
41+ * Typing "/quit" closes the session gracefully.
4242 *
43- * Intended Usage
44- * --------------
45- * This minimal client is ideal for:
46- * • Testing or debugging a Vix.cpp WebSocket server
47- * • Learning how to work with the JSON protocol
48- * • Building simple chat tools or monitoring utilities
49- * • Demonstrating the basics of WebSocket event handling in C++
43+ * Intended Usage
44+ * --------------
45+ * This minimal client is ideal for:
46+ * - Testing or debugging a Vix.cpp WebSocket server
47+ * - Learning how to work with the JSON protocol
48+ * - Building simple chat tools or monitoring utilities
49+ * - Demonstrating the basics of WebSocket event handling in C++
5050 *
51- * How to Run
52- * ----------
53- * 1. Start a Vix WebSocket server (see simple_server.cpp).
54- * 2. Build this example:
55- * cmake -S . -B build && cmake --build build -j
56- * 3. Run the client:
57- * ./build/examples/simple/simple_client
58- * 4. Type messages interactively, or use "/quit" to exit.
51+ * How to Run
52+ * ----------
53+ * 1. Start a Vix WebSocket server (see simple_server.cpp).
54+ * 2. Build this example:
55+ * cmake -S . -B build && cmake --build build -j
56+ * 3. Run the client:
57+ * ./build/examples/simple/simple_client
58+ * 4. Type messages interactively, or use "/quit" to exit.
5959 *
60- * This is a deliberately minimal example—see the advanced client example for
61- * support for rooms, reconnection logic, structured system events, and
62- * persistent message workflows.
60+ * This is a deliberately minimal example. See the advanced client example for
61+ * support for rooms, reconnection logic, structured system events, and
62+ * persistent message workflows.
6363 */
6464
65+ #include < chrono>
6566#include < iostream>
6667#include < string>
6768
@@ -75,64 +76,80 @@ int main()
7576
7677 auto client = Client::create (" localhost" , " 9090" , " /" );
7778
78- client->on_open ([]
79- { std::cout << " [client] Connected ✅" << std::endl; });
79+ client->on_open (
80+ []
81+ {
82+ std::cout << " [client] Connected ✅" << std::endl;
83+ });
8084
81- client->on_message ([](const std::string &msg)
82- {
85+ client->on_message (
86+ [](const std::string &msg)
87+ {
8388 auto jm = JsonMessage::parse (msg);
8489
8590 if (!jm)
8691 {
87- std::cout << msg << std::endl;
88- return ;
92+ std::cout << msg << std::endl;
93+ return ;
8994 }
9095
9196 const std::string &type = jm->type ;
9297
9398 if (type == " chat.system" )
9499 {
95- std::cout << " [system] " << jm->get_string (" text" ) << std::endl;
100+ std::cout << " [system] " << jm->get_string (" text" ) << std::endl;
96101 }
97102 else if (type == " chat.message" )
98103 {
99- std::string user = jm->get_string (" user" );
100- if (user.empty ())
101- user = " anonymous" ;
102-
103- std::cout << " [chat] " << user
104- << " : " << jm->get_string (" text" ) << std::endl;
104+ std::string user = jm->get_string (" user" );
105+ if (user.empty ())
106+ {
107+ user = " anonymous" ;
108+ }
109+
110+ std::cout << " [chat] " << user
111+ << " : " << jm->get_string (" text" ) << std::endl;
105112 }
106113 else
107114 {
108- std::cout << msg << std::endl;
109- } });
115+ std::cout << msg << std::endl;
116+ }
117+ });
110118
111- client->on_close ([]
112- { std::cout << " [client] Disconnected." << std::endl; });
119+ client->on_close (
120+ []
121+ {
122+ std::cout << " [client] Disconnected." << std::endl;
123+ });
113124
114- client->on_error ([](const std::string &error)
115- { std::cerr << " [client] error: " << error << std::endl; });
125+ client->on_error (
126+ [](const std::string &error)
127+ {
128+ std::cerr << " [client] error: " << error << std::endl;
129+ });
116130
117131 client->enable_auto_reconnect (true , std::chrono::seconds (3 ));
118132 client->enable_heartbeat (std::chrono::seconds (20 ));
119133
120134 client->connect ();
121135
122- // Prompt username
123136 std::cout << " Pseudo: " ;
124137 std::string user;
125138 std::getline (std::cin, user);
139+
126140 if (user.empty ())
141+ {
127142 user = " anonymous" ;
143+ }
128144
129145 std::cout << " Type messages, /quit to exit\n " ;
130146
131- // Message loop
132147 for (std::string line; std::getline (std::cin, line);)
133148 {
134149 if (line == " /quit" )
150+ {
135151 break ;
152+ }
136153
137154 client->send (
138155 " chat.message" ,
0 commit comments