fewd/priv/static/js/ts/webrtc.ts
Peter Harpending db6d244cb6 finished code surgery
patient is comatose and is missing both testicles and eyeballs but is legally
alive
2026-02-12 19:31:08 -08:00

67 lines
1.5 KiB
TypeScript

/**
* webrtc page script
*
* Author: Peter Harpending <peterharpending@qpq.swiss>
* Date: 2026-02-04
* Copyright: Copyright (c) 2026 QPQ AG
*
* Reference: https://git.qpq.swiss/QPQ-AG/research-megadoc/src/commit/c7c4592d4b21ad120145ef63334471a1a7ec1e60/paste/2026-02/grok-webrtc.html
*
* @module
*/
main();
async function
main
()
: Promise<void>
{
// start websocket immediately
let ws: WebSocket;
let init = document.getElementById('init') as HTMLDivElement;
let init_join = document.getElementById('init-join') as HTMLButtonElement;
// handle button click
init_join.addEventListener('click',
function() {
init.hidden = true;
ws = new WebSocket('/ws/webrtc');
ws.onopen = function(e) { console.log('ws open'); };
ws.onclose = function(e) { console.warn('ws closed'); };
ws.onerror = function(e) { console.error('ws error:', e); };
ws.onmessage =
function(e) {
// console.log('ws message:', e.data);
let message = JSON.parse(e.data) as ws_msg;
handle_ws_msg(message);
};
}
);
}
type ws_msg = ["join", string]
| ["down", string];
function
handle_ws_msg
(message: ws_msg)
: void
{
console.log('message from server:', message);
}
function
ws_send_json
(ws : WebSocket,
x : any)
: void
{
let s: string = JSON.stringify(x, undefined, 4);
console.log('sending:\n', s);
ws.send(s);
}