Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,10 @@ if (VIX_ENABLE_INSTALL)
# Install headers if present
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/modules/core/include")
install(DIRECTORY modules/core/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
PATTERN "*.inc")
endif()

if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/modules/utils/include")
Expand Down
2 changes: 1 addition & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"port": 8080,
"request_timeout": 2000,
"io_threads": 0,
"session_timeout_sec": 120
"session_timeout_sec": 300
},
"logging": {
"async": true,
Expand Down
14 changes: 14 additions & 0 deletions examples/01_run_blocking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <vix.hpp>

using namespace vix;

int main()
{
App app;

app.get("/", [](Request &, Response &res)
{ res.text("Hello Vix"); });

// Bloque le thread courant
app.run(8080);
}
19 changes: 19 additions & 0 deletions examples/02_listen_background.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <vix.hpp>
#include <iostream>

using namespace vix;

int main()
{
App app;

app.get("/health", [](Request &, Response &res)
{ res.json({"ok", true}); });

// Lance le serveur en background
app.listen(8080, []()
{ std::cout << "Server is ready and accepting connections\n"; });

// Attente propre
app.wait();
}
18 changes: 18 additions & 0 deletions examples/03_listen_port_dynamic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <vix.hpp>
#include <iostream>

using namespace vix;

int main()
{
App app;

app.get("/", [](Request &, Response &res)
{ res.text("Dynamic port example"); });

// Port 0 → choisi par l’OS
app.listen_port(0, [](int port)
{ std::cout << "Listening on http://localhost:" << port << "\n"; });

app.wait();
}
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Vix examples

01_run_blocking.cpp
- Minimal blocking server using `run()`

02_listen_background.cpp
- Non-blocking server using `listen()` and `wait()`

03_listen_port_dynamic.cpp
- Dynamic port binding (port = 0) using `listen_port()`
4 changes: 2 additions & 2 deletions examples/p2p/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ int main()
app.get("/connect", [](Request &, Response &res)
{ res.file("./public/connect.html"); });

app.listen(5178, [](const vix::utils::ServerReadyInfo &info)
{ console.info("UI API listening on", info.port); });
app.listen(5178, []()
{ console.info("UI API listening on", 5178); });

app.wait();
runtime.stop();
Expand Down
9 changes: 4 additions & 5 deletions examples/session/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ int main()
App app;

app.use(middleware::app::adapt_ctx(
middleware::auth::session({.secret = "dev"})
));
middleware::auth::session({.secret = "dev"})));

app.get("/session", [](Request &req, Response &res){
app.get("/session", [](Request &req, Response &res)
{
auto &s = req.state<middleware::auth::Session>();

int n = s.get("n") ? std::stoi(*s.get("n")) : 0;
s.set("n", std::to_string(++n));

res.text("n=" + std::to_string(n));
});
res.text("n=" + std::to_string(n)); });

app.run(8080);
}
61 changes: 0 additions & 61 deletions examples/websocket/advanced/CMakeLists.txt

This file was deleted.

161 changes: 161 additions & 0 deletions examples/websocket/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<title>Vix WebSocket + LongPolling Client</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
}
#log {
background: #111;
color: #0f0;
height: 300px;
overflow-y: auto;
padding: 10px;
}
input,
button {
padding: 6px;
margin: 5px 0;
}
</style>
</head>
<body>
<h2>Vix.cpp Client — WebSocket + Long Polling</h2>

<label>Pseudo:</label>
<input id="user" value="web-user" />

<label>Room:</label>
<input id="room" value="general" />

<hr />

<button id="connectWS">Connect WebSocket</button>
<button id="sendWS" disabled>Send WS</button>

<br />

<button id="startLP">Start Long-Poll Session</button>
<button id="sendLP" disabled>Send LP</button>

<hr />

<input id="msg" placeholder="Message..." size="40" />

<h3>Logs</h3>
<pre id="log"></pre>

<script>
function log(x) {
const el = document.getElementById("log");
el.textContent += x + "\n";
el.scrollTop = el.scrollHeight;
}

// 1) WebSocket client
let ws;

document.getElementById("connectWS").onclick = () => {
ws = new WebSocket("ws://127.0.0.1:9090/chat");

ws.onopen = () => {
log("✔️ WS connected");
document.getElementById("sendWS").disabled = false;
};

ws.onmessage = (ev) => log("📩 WS: " + ev.data);
ws.onerror = (ev) => log("❌ WS error: " + ev);
ws.onclose = () => {
log("❌ WS closed");
document.getElementById("sendWS").disabled = true;
};
};

document.getElementById("sendWS").onclick = () => {
const user = document.getElementById("user").value;
const room = document.getElementById("room").value;
const text = document.getElementById("msg").value;

if (!text) return;

const json = {
type: "chat.message",
room: room,
payload: { user, text },
};

ws.send(JSON.stringify(json));
log("➡️ WS sent: " + JSON.stringify(json));
document.getElementById("msg").value = "";
};

// 2) Long Polling client
let sessionId = null;
let lpActive = false;

async function pollLoop() {
while (lpActive) {
try {
const url = `/ws/poll?session_id=${sessionId}&max=50`;

const res = await fetch(url);
if (!res.ok) {
log("❌ LP poll error: " + res.status);
await new Promise((r) => setTimeout(r, 1000));
continue;
}

const arr = await res.json();

for (const msg of arr) {
log("📩 LP: " + JSON.stringify(msg));
}
} catch (e) {
log("❌ LP poll exception: " + e);
}

// Petite pause minimum (LP "standard")
await new Promise((r) => setTimeout(r, 100));
}
}

document.getElementById("startLP").onclick = () => {
sessionId = crypto.randomUUID();
log("📌 LP session_id = " + sessionId);

lpActive = true;
document.getElementById("sendLP").disabled = false;

pollLoop();
};

document.getElementById("sendLP").onclick = async () => {
const user = document.getElementById("user").value;
const room = document.getElementById("room").value;
const text = document.getElementById("msg").value;

if (!text) return;

const body = {
session_id: sessionId,
type: "chat.message",
room: room,
payload: { user, text },
};

const res = await fetch("/ws/send", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});

log("➡️ LP sent: " + JSON.stringify(body));

document.getElementById("msg").value = "";
};
</script>
</body>
</html>
Loading
Loading