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>
6665#include < iostream>
6766#include < string>
6867
@@ -76,80 +75,64 @@ int main()
7675
7776 auto client = Client::create (" localhost" , " 9090" , " /" );
7877
79- client->on_open (
80- []
81- {
82- std::cout << " [client] Connected ✅" << std::endl;
83- });
78+ client->on_open ([]
79+ { std::cout << " [client] Connected ✅" << std::endl; });
8480
85- client->on_message (
86- [](const std::string &msg)
87- {
81+ client->on_message ([](const std::string &msg)
82+ {
8883 auto jm = JsonMessage::parse (msg);
8984
9085 if (!jm)
9186 {
92- std::cout << msg << std::endl;
93- return ;
87+ std::cout << msg << std::endl;
88+ return ;
9489 }
9590
9691 const std::string &type = jm->type ;
9792
9893 if (type == " chat.system" )
9994 {
100- std::cout << " [system] " << jm->get_string (" text" ) << std::endl;
95+ std::cout << " [system] " << jm->get_string (" text" ) << std::endl;
10196 }
10297 else if (type == " chat.message" )
10398 {
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;
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;
112105 }
113106 else
114107 {
115- std::cout << msg << std::endl;
116- }
117- });
108+ std::cout << msg << std::endl;
109+ } });
118110
119- client->on_close (
120- []
121- {
122- std::cout << " [client] Disconnected." << std::endl;
123- });
111+ client->on_close ([]
112+ { std::cout << " [client] Disconnected." << std::endl; });
124113
125- client->on_error (
126- [](const std::string &error)
127- {
128- std::cerr << " [client] error: " << error << std::endl;
129- });
114+ client->on_error ([](const std::string &error)
115+ { std::cerr << " [client] error: " << error << std::endl; });
130116
131117 client->enable_auto_reconnect (true , std::chrono::seconds (3 ));
132118 client->enable_heartbeat (std::chrono::seconds (20 ));
133119
134120 client->connect ();
135121
122+ // Prompt username
136123 std::cout << " Pseudo: " ;
137124 std::string user;
138125 std::getline (std::cin, user);
139-
140126 if (user.empty ())
141- {
142127 user = " anonymous" ;
143- }
144128
145129 std::cout << " Type messages, /quit to exit\n " ;
146130
131+ // Message loop
147132 for (std::string line; std::getline (std::cin, line);)
148133 {
149134 if (line == " /quit" )
150- {
151135 break ;
152- }
153136
154137 client->send (
155138 " chat.message" ,
0 commit comments