mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-16 09:05:28 +00:00
feat(DevWeb): Add WebSocket in MainMenu to display user list dynamically
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
<script src="${pageContext.request.contextPath}/static/js/modal.js" defer></script>
|
||||
<script src="${pageContext.request.contextPath}/static/js/new-game.js" defer></script>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<script defer src="${pageContext.request.contextPath}/static/js/main-menu.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<%@include file="../components/navbar.jsp"%>
|
||||
|
||||
@@ -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) => {}
|
||||
Reference in New Issue
Block a user