export function initTchat() {
    const socketTchat = new WebSocket("ws://" + window.location.host + "/Projet/ws/tchat");

    socketTchat.onmessage = event => {
        const container = document.querySelector(".messages-container");
        if (container) {
            const p = document.createElement("p");
            p.textContent = event.data;
            container.appendChild(p);
            container.scrollTop = container.scrollHeight;
        }
    };

    const input = document.querySelector("input[name='contenu']");
    if (input) {
        input.addEventListener("keydown", event => {
            const login = document.getElementById("user-login").textContent;
            if (event.key === "Enter" && input.value.trim() !== "") {
                socketTchat.send(`${login} : ${input.value}`);
                input.value = "";
            }
        });
    }
}
