have a roster and a whoami

This commit is contained in:
Peter Harpending
2026-02-12 20:32:34 -08:00
parent db6d244cb6
commit 08c0119ae0
6 changed files with 102 additions and 17 deletions
+28
View File
@@ -10,6 +10,8 @@
*
* @module
*/
let st = { whoami: "",
roster: [] };
main();
async function main() {
// start websocket immediately
@@ -19,6 +21,7 @@ async function main() {
// handle button click
init_join.addEventListener('click', function () {
init.hidden = true;
document.getElementById('run').hidden = false;
ws = new WebSocket('/ws/webrtc');
ws.onopen = function (e) { console.log('ws open'); };
ws.onclose = function (e) { console.warn('ws closed'); };
@@ -33,10 +36,35 @@ async function main() {
}
function handle_ws_msg(message) {
console.log('message from server:', message);
switch (message[0]) {
case "whoami":
st.whoami = message[1];
break;
case "roster":
st.roster = message[1];
break;
default:
console.warn("unknown message", message);
}
render_state();
}
function ws_send_json(ws, x) {
let s = JSON.stringify(x, undefined, 4);
console.log('sending:\n', s);
ws.send(s);
}
function render_state() {
console.log('st', st);
// whoami
document.getElementById('whoami').innerText = st.whoami;
//
let roster_ul = document.getElementById('roster-ul');
let newChildren = [];
for (let nick of st.roster) {
let li = document.createElement('li');
li.innerText = nick;
newChildren.push(li);
}
roster_ul.replaceChildren(...newChildren);
}
//# sourceMappingURL=webrtc.js.map