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
18 changes: 11 additions & 7 deletions examples/websocket/advanced/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# ------------------------------------------------------------------------------
# CMakeLists.txt — Advanced WebSocket Examples for Vix.cpp
# @file CMakeLists.cpp
# @author Gaspard Kirira
#
# Copyright 2025, Gaspard Kirira. All rights reserved.
# https://github.com/vixcpp/vix
# Use of this source code is governed by a MIT license
# that can be found in the License file.
#
# Vix.cpp
#
# Advanced WebSocket Examples for Vix.cpp
#
# This CMake file builds two advanced demonstration executables showcasing
# production-style usage of the Vix.cpp WebSocket module:
Expand Down Expand Up @@ -30,10 +40,7 @@ project(vix_ws_advanced LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ------------------------------------------------------------------------------
# Advanced WebSocket Server
# ------------------------------------------------------------------------------

add_executable(vix-ws-advanced-server
src/server.cpp
)
Expand All @@ -43,10 +50,7 @@ target_link_libraries(vix-ws-advanced-server
vix::websocket
)

# ------------------------------------------------------------------------------
# Advanced WebSocket Client
# ------------------------------------------------------------------------------

add_executable(vix-ws-advanced-client
src/client.cpp
)
Expand Down
31 changes: 5 additions & 26 deletions examples/websocket/advanced/advanced_chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="UTF-8" />
<title>Vix WebSocket Rooms Chat</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- (CSS identique à la version précédente, je le laisse intact pour garder le style) -->
<style>
:root {
--bg: #050816;
Expand Down Expand Up @@ -547,33 +546,26 @@ <h1>Vix WebSocket Chat</h1>
</div>

<script>
// --- Configuration ---
const WS_PORT = 9091; // WebSocket port (from config.json)
const HTTP_PORT = 8080; // HTTP app port (vix::App)
const WS_PORT = 9091;
const HTTP_PORT = 8080;

// Base HTTP pour /ws/poll et /ws/send
// Si tu ouvres le fichier en file:// ou depuis un autre port,
// on force les appels API vers le backend Vix (8080).
const host = window.location.hostname || "localhost";
const HTTP_BASE =
window.location.protocol === "https:"
? `https://${host}:${HTTP_PORT}`
: `http://${host}:${HTTP_PORT}`;

// --- State ---
let ws = null;
let currentUser = "anonymous";
let currentRoom = "general";
let connectedWS = false;
let lpMode = false; // vrai si on est en mode LP
let lpActive = false; // boucle LP en cours ou pas
let lpMode = false;
let lpActive = false;
let activityCount = 0;

// Session ID LP pour /ws/poll et /ws/send
let lpSessionId =
"lp-" + Math.random().toString(36).slice(2) + Date.now().toString(36);

// --- DOM ---
const statusDot = document.getElementById("statusDot");
const statusText = document.getElementById("statusText");
const connectBtn = document.getElementById("connectBtn");
Expand All @@ -587,7 +579,6 @@ <h1>Vix WebSocket Chat</h1>
const activityLog = document.getElementById("activityLog");
const activityCounter = document.getElementById("activityCounter");

// --- Utils ---
function formatTime(d) {
return d.toLocaleTimeString(undefined, {
hour: "2-digit",
Expand Down Expand Up @@ -626,7 +617,6 @@ <h1>Vix WebSocket Chat</h1>
}
}

// --- Messages UI ---
function addMessage({ kind, type, room, user, text }) {
const container = document.createElement("div");
container.classList.add("message");
Expand Down Expand Up @@ -717,7 +707,6 @@ <h1>Vix WebSocket Chat</h1>
handleJsonEnvelope(obj);
}
} catch (e) {
// Fallback texte brut
addMessage({
kind: "system",
type: "raw",
Expand All @@ -728,7 +717,6 @@ <h1>Vix WebSocket Chat</h1>
}
}

// --- Envoi WebSocket / LongPolling ---
function sendViaWS(type, payload) {
if (!ws || ws.readyState !== WebSocket.OPEN) return;
const message = { type, payload: payload || {} };
Expand Down Expand Up @@ -771,7 +759,6 @@ <h1>Vix WebSocket Chat</h1>
}
}

// --- LongPolling loop ---
async function longPollLoop() {
lpActive = true;
appendActivity(
Expand Down Expand Up @@ -834,7 +821,6 @@ <h1>Vix WebSocket Chat</h1>
updateConnectionStatus();
}

// --- WebSocket connect ---
function connectWS() {
const wsHost = host || "localhost";
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
Expand All @@ -846,11 +832,10 @@ <h1>Vix WebSocket Chat</h1>

ws.onopen = () => {
connectedWS = true;
stopLP(); // on coupe LP si jamais il tournait
stopLP();
updateConnectionStatus();
appendActivity("WebSocket connection opened", true);

// join room courante
sendViaWS("chat.join", {
room: currentRoom,
user: currentUser,
Expand All @@ -869,7 +854,6 @@ <h1>Vix WebSocket Chat</h1>
false
);

// Fallback LP
if (!lpMode) {
startLP();
}
Expand All @@ -881,7 +865,6 @@ <h1>Vix WebSocket Chat</h1>
};
}

// --- Connexion principale ---
function connect() {
currentUser = userInput.value.trim() || "anonymous";
currentRoom = roomInput.value.trim() || "general";
Expand All @@ -895,7 +878,6 @@ <h1>Vix WebSocket Chat</h1>
function joinRoom(newRoom) {
if (!newRoom) return;

// leave ancienne room
sendMessage("chat.leave", {
room: currentRoom,
user: currentUser,
Expand All @@ -904,7 +886,6 @@ <h1>Vix WebSocket Chat</h1>
currentRoom = newRoom;
currentRoomLabel.textContent = currentRoom;

// join nouvelle room
sendMessage("chat.join", {
room: currentRoom,
user: currentUser,
Expand All @@ -927,7 +908,6 @@ <h1>Vix WebSocket Chat</h1>
composerInput.focus();
}

// --- Events UI ---
connectBtn.addEventListener("click", () => {
if (connectedWS || lpMode) return;
connect();
Expand All @@ -952,7 +932,6 @@ <h1>Vix WebSocket Chat</h1>
}
});

// Valeurs par défaut
userInput.value = "alice";
roomInput.value = "africa";
currentUserLabel.textContent = "—";
Expand Down
Loading
Loading