add basic session & credential dealing

This commit is contained in:
ducklet 2021-01-30 13:41:25 +01:00
parent f6bf544f54
commit f4cf26a33e
7 changed files with 128 additions and 52 deletions

View file

@ -1,5 +1,10 @@
"use strict"
/* global document, window */
const crypto = window.crypto
const location = document.location
const storage = window.sessionStorage
// TODOs
// - measure/report latency
// - use server reported time to find winner
@ -44,7 +49,8 @@ let socket,
servertime,
toffset_ms,
clients = [],
me
me,
session_key
function hide(e) {
q(`#${e}`).style.display = "none"
@ -60,7 +66,7 @@ function session_id() {
}
function new_session_id() {
if (!window.crypto) {
if (!crypto) {
return Math.random().toString(36).substr(2)
}
const data = new Uint8Array(10)
@ -96,7 +102,7 @@ function redraw_clients(me, clients) {
text: c.name,
data: { cid: c.id },
appendTo: ul,
cls: c.id === me ? "me" : "",
cls: c.id === me.id ? "me" : "",
})
}
}
@ -193,8 +199,14 @@ function setup_ws() {
servertime = msg.value
toffset_ms = performance.now()
} else if (msg.type === "id") {
me = msg.value
me = { id: msg.id, key: msg.key }
storage["my_id"] = me.id
storage["my_key"] = me.key
redraw_clients(me, clients)
} else if (msg.type === "session_key") {
session_key = { path: msg.path, key: msg.key }
storage["session_path"] = session_key.path
storage["session_key"] = session_key.key
} else if (msg.type === "buzz") {
const buzztime_ns = msg.time
const client_id = msg.client