feat(DevWeb): Add WebSocket in MainMenu to display user list dynamically

This commit is contained in:
Lucàs
2024-04-15 13:10:01 +02:00
parent 30163840d8
commit 7966217125
8 changed files with 125 additions and 12 deletions
@@ -10,14 +10,9 @@ loginForm.addEventListener("submit", (event) => {
fetch(loginForm.getAttribute("action"), {
headers: {"Content-Type": "application/json"}, body: JSON.stringify(data), method: loginForm.getAttribute("method"),
})
.then(res => {
if (res.ok) {
window.location.href = "./main-menu";
}
else {
// TODO Display red inputs
}
})
.then(res => res.json())
.then(user => sessionStorage.setItem("user", JSON.stringify(user)))
.then(() => window.location.href = "./main-menu")
.catch(error => console.error("Error:", error));
});
@@ -0,0 +1,25 @@
const session = JSON.parse(sessionStorage.getItem('session'));
url = new URL(window.location.href);
url.protocol = 'ws:';
const websocket = new WebSocket(url);
websocket.onopen = () => {
const linkUserSession = {
type: 'linkUserSession',
data: sessionStorage.getItem('user')
}
websocket.send(JSON.stringify(linkUserSession));
}
websocket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'userList') {
console.log(JSON.parse(data.data));
}
}
websocket.onclose = () => {}
websocket.onerror = (error) => {}