Skip to content

Commit 84decba

Browse files
committed
refactor(websocket): migrate examples to RuntimeExecutor
1 parent 94961f6 commit 84decba

8 files changed

Lines changed: 108 additions & 139 deletions

File tree

examples/advanced/src/server.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
* Vix.cpp
1212
*
1313
* This example demonstrates a fully featured, production-style WebSocket
14-
* server using the Vix.cpp executor abstraction. It showcases how to combine:
14+
* server using the Vix.cpp runtime. It showcases how to combine:
1515
*
1616
* • Asynchronous native WebSocket server
17-
* • Thread pool executor integration
17+
* • RuntimeExecutor integration
1818
* • Room-based messaging (join, leave, broadcast)
1919
* • Typed JSON protocol ("type" + "payload")
2020
* • Persistent message storage using SQLite (WAL enabled)
@@ -37,7 +37,7 @@
3737

3838
#include <vix.hpp>
3939

40-
#include <vix/experimental/ThreadPoolExecutor.hpp>
40+
#include <vix/executor/RuntimeExecutor.hpp>
4141
#include <vix/websocket.hpp>
4242
#include <vix/websocket/LongPolling.hpp>
4343
#include <vix/websocket/LongPollingBridge.hpp>
@@ -57,13 +57,9 @@ int main()
5757

5858
using njson = nlohmann::json;
5959

60-
auto exec = vix::experimental::make_threadpool_executor(
61-
4, // min threads
62-
8, // max threads
63-
0 // default priority
64-
);
60+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
6561

66-
App wsApp{"config/config.json", std::move(exec)};
62+
App wsApp{"config/config.json", exec};
6763
auto &ws = wsApp.server();
6864

6965
WebSocketMetrics metrics;

examples/chat_room.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//
4545

4646
#include <vix/config/Config.hpp>
47-
#include <vix/experimental/ThreadPoolExecutor.hpp>
47+
#include <vix/executor/RuntimeExecutor.hpp>
4848
#include <vix/websocket.hpp>
4949

5050
int main()
@@ -54,15 +54,11 @@ int main()
5454
// 1) Load config
5555
vix::config::Config cfg{"config/config.json"};
5656

57-
// 2) Thread pool executor for async WebSocket processing
58-
auto exec = vix::experimental::make_threadpool_executor(
59-
4, // min threads
60-
8, // max threads
61-
0 // default priority
62-
);
57+
// 2) Runtime executor for async WebSocket processing
58+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
6359

6460
// 3) Construct the WebSocket server
65-
Server ws(cfg, std::move(exec));
61+
Server ws(cfg, exec);
6662

6763
// 4) On new connection
6864
ws.on_open(

examples/simple/src/app_example.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* Vix.cpp
1212
*/
1313
#include <iostream>
14+
#include <memory>
1415

15-
#include <vix/experimental/ThreadPoolExecutor.hpp>
16+
#include <vix/executor/RuntimeExecutor.hpp>
1617
#include <vix/websocket.hpp>
1718

1819
using vix::websocket::App;
@@ -39,13 +40,9 @@ void handle_chat(
3940

4041
int main()
4142
{
42-
auto exec = vix::experimental::make_threadpool_executor(
43-
4, // min threads
44-
8, // max threads
45-
0 // default priority
46-
);
43+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
4744

48-
App app{"config/config.json", std::move(exec)};
45+
App app{"config/config.json", exec};
4946

5047
(void)app.ws("/chat", handle_chat);
5148

examples/simple/src/minimal_ws_server.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,21 @@
1111
* Vix.cpp
1212
*/
1313
#include <iostream>
14+
#include <memory>
1415
#include <string>
1516

1617
#include <vix/config/Config.hpp>
17-
#include <vix/experimental/ThreadPoolExecutor.hpp>
18+
#include <vix/executor/RuntimeExecutor.hpp>
1819
#include <vix/websocket.hpp>
1920
#include <vix/websocket/protocol.hpp>
2021

2122
namespace ws = vix::websocket;
2223

2324
int main()
2425
{
25-
auto exec = vix::experimental::make_threadpool_executor(
26-
4, // min threads
27-
8, // max threads
28-
0 // default priority
29-
);
26+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
3027

31-
ws::App app{"config/config.json", std::move(exec)};
28+
ws::App app{"config/config.json", exec};
3229
auto &server = app.server();
3330

3431
std::cout << "[minimal] WebSocket server starting on port "

examples/simple/src/simple_client.cpp

Lines changed: 63 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,59 @@
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",

examples/simple/src/simple_server.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* simplest reference implementation, showing only the essential components:
1717
*
1818
* • Loading configuration (port, timeouts, etc.)
19-
* • Creating a thread pool executor for async execution
19+
* • Creating a RuntimeExecutor for async execution
2020
* • Starting a WebSocket server instance
2121
* • Reacting to connection events (on_open)
2222
* • Handling typed JSON messages (on_typed_message)
@@ -28,9 +28,9 @@
2828
* The WebSocket server automatically binds to the port defined in
2929
* config/config.json and manages all asynchronous I/O.
3030
*
31-
* 2. Executor Integration:
32-
* The example uses a thread pool executor to drive async execution,
33-
* aligning the WebSocket layer with the Vix executor abstraction.
31+
* 2. Runtime Integration:
32+
* The example uses Vix’s RuntimeExecutor to drive async execution,
33+
* aligning the WebSocket layer with the modern Vix runtime architecture.
3434
*
3535
* 3. Global Broadcast:
3636
* Messages received with type "chat.message" are broadcast to all
@@ -67,8 +67,10 @@
6767
* persistence, metrics, room routing, history replay, and auto-reconnect.
6868
*/
6969

70+
#include <memory>
71+
7072
#include <vix/config/Config.hpp>
71-
#include <vix/experimental/ThreadPoolExecutor.hpp>
73+
#include <vix/executor/RuntimeExecutor.hpp>
7274
#include <vix/websocket.hpp>
7375

7476
int main()
@@ -78,14 +80,10 @@ int main()
7880
// Load configuration from config/config.json
7981
vix::config::Config cfg{"config/config.json"};
8082

81-
// Thread pool executor for async work
82-
auto exec = vix::experimental::make_threadpool_executor(
83-
4, // min threads
84-
8, // max threads
85-
0 // default priority
86-
);
83+
// Runtime executor for async work
84+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>();
8785

88-
Server ws(cfg, std::move(exec));
86+
Server ws(cfg, exec);
8987

9088
// On new connection: broadcast a welcome system message
9189
ws.on_open(

0 commit comments

Comments
 (0)