Files
games/Web/game-realtime-webtransport-browser-smoke/frontend/ts/main.ts
2026-09-22 06:36:54 +02:00

70 lines
2.6 KiB
TypeScript

// file: Web/game-realtime-webtransport-browser-smoke/frontend/ts/main.ts
// version: 1
import init, { run_browser_smoke } from "@webtransport-browser-smoke-wasm";
function requireElement<T extends Element>(selector: string): T {
const element = document.querySelector<T>(selector);
if (element === null) {
throw new Error(`Required browser smoke element is missing: ${selector}`);
}
return element;
}
function populateFromQuery(endpoint: HTMLInputElement, certificateHash: HTMLInputElement): void {
const parameters = new URLSearchParams(window.location.search);
const endpointValue = parameters.get("endpoint");
const hashValue = parameters.get("sha256");
if (endpointValue !== null) {
endpoint.value = endpointValue;
}
if (hashValue !== null) {
certificateHash.value = hashValue;
}
}
async function main(): Promise<void> {
const environmentStatus = requireElement<HTMLParagraphElement>("#environment-status");
const endpoint = requireElement<HTMLInputElement>("#endpoint");
const certificateHash = requireElement<HTMLInputElement>("#certificate-hash");
const run = requireElement<HTMLButtonElement>("#run");
const result = requireElement<HTMLPreElement>("#result");
populateFromQuery(endpoint, certificateHash);
if (!window.isSecureContext) {
environmentStatus.textContent = "FAIL: page is not running in a secure context";
run.disabled = true;
return;
}
if (!("WebTransport" in window)) {
environmentStatus.textContent = "FAIL: this browser does not expose WebTransport";
run.disabled = true;
return;
}
await init();
environmentStatus.textContent = "Secure context and WebTransport available; WASM loaded";
run.addEventListener("click", () => {
run.disabled = true;
result.textContent = "RUNNING";
void run_browser_smoke(endpoint.value.trim(), certificateHash.value.trim())
.then(message => {
result.textContent = message;
})
.catch(caughtError => {
result.textContent = `game-realtime-webtransport-browser-smoke: FAIL: ${String(caughtError)}`;
})
.finally(() => {
run.disabled = false;
});
});
if (endpoint.value.length > 0 && certificateHash.value.length > 0) {
run.click();
}
}
void main().catch(caughtError => {
const result = document.querySelector<HTMLPreElement>("#result");
if (result !== null) {
result.textContent = `game-realtime-webtransport-browser-smoke: FAIL: ${String(caughtError)}`;
}
});