32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
const ws_url = new URL('ws', window.location.href);
|
|
ws_url.protocol = ws_url.protocol.replace('http', 'ws');
|
|
const ws = new WebSocket(ws_url.href);
|
|
|
|
ws.onmessage = function(event) {
|
|
const msg = JSON.parse(event.data);
|
|
|
|
console.log(msg);
|
|
|
|
if (msg.hasOwnProperty('currentsession')) {
|
|
document.getElementById('current_session').innerText = msg.currentsession;
|
|
}
|
|
if (msg.hasOwnProperty('username')) {
|
|
document.getElementById('greeting').innerText = "Hello, " + msg.username + "!";
|
|
}
|
|
|
|
if (msg.hasOwnProperty('allsessions')) {
|
|
const all_sessions = document.getElementById('all_sessions');
|
|
|
|
while (all_sessions.children.length) all_sessions.lastChild.remove();
|
|
|
|
for (let session of msg.allsessions) {
|
|
const button = document.createElement('button');
|
|
button.innerText = session;
|
|
button.onclick = async (e) => await fetch('api/join_session', {
|
|
method: 'POST', body: JSON.stringify({sessionid: session})
|
|
});
|
|
all_sessions.appendChild(button);
|
|
}
|
|
}
|
|
};
|