From b6436c84eed2f3763a60b07786e48aa6c8bbc71d Mon Sep 17 00:00:00 2001 From: Peter Harpending Date: Mon, 9 Feb 2026 14:48:47 -0800 Subject: [PATCH] wip --- priv/static/js/ts/webrtc.ts | 57 +++++++++++++++++++++++++++++++++++-- priv/static/webrtc.html | 11 ++++--- src/fd_httpd_client.erl | 29 +++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) diff --git a/priv/static/js/ts/webrtc.ts b/priv/static/js/ts/webrtc.ts index dfe998c..a5c1adb 100644 --- a/priv/static/js/ts/webrtc.ts +++ b/priv/static/js/ts/webrtc.ts @@ -13,8 +13,61 @@ main(); async function -main() +main + () + : Promise { - let pc: RTCPeerConnection = new RTCPeerConnection(); + // start websocket immediately + let ws = new WebSocket('/ws/webrtc'); + + // grab document elements + let init = document.getElementById('init') as HTMLDivElement; + let peers = document.getElementById('peers') as HTMLDivElement; + + let init_name = document.getElementById('init-name') as HTMLInputElement; + let init_join = document.getElementById('init-join') as HTMLButtonElement; + + // handle button click + init_join.addEventListener('click', + function() { + handle_join(init, init_name, peers, ws); + } + ); + + // handle message from ws + ws.onopen = function(e) { console.log('ws open:', e); }; + ws.onclose = function(e) { console.log('ws closed:', e); }; + ws.onerror = function(e) { console.error('ws error:', e); }; + ws.onmessage = function(e) { console.log('ws message', e); }; } +async function +handle_join + (init : HTMLDivElement, + init_name : HTMLInputElement, + peers : HTMLDivElement, + ws : WebSocket) + : Promise +{ + console.log('connecting...'); + let user_name: string = init_name.value.trim(); + console.log('username:', user_name); + + ws_send_json(ws, ['username', user_name]); + + init.hidden = true; + peers.hidden = false; +} + + +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); +} diff --git a/priv/static/webrtc.html b/priv/static/webrtc.html index 68611c7..fd9603e 100644 --- a/priv/static/webrtc.html +++ b/priv/static/webrtc.html @@ -16,13 +16,16 @@

FEWD: webrtc demo

- - - + + +
+ diff --git a/src/fd_httpd_client.erl b/src/fd_httpd_client.erl index e163dae..87550b8 100644 --- a/src/fd_httpd_client.erl +++ b/src/fd_httpd_client.erl @@ -233,6 +233,7 @@ handle_request(Sock, R = #request{method = M, path = P}, Received) when M =/= un route(Sock, get, Route, Request, Received) -> case Route of <<"/ws/echo">> -> ws_echo(Sock, Request) , Received; + <<"/ws/webrtc">> -> ws_webrtc(Sock, Request) , Received; <<"/">> -> route_static(Sock, <<"/index.html">>) , Received; _ -> route_static(Sock, Route) , Received end; @@ -279,6 +280,34 @@ respond_static(Sock, not_found) -> fd_httpd_utils:http_err(Sock, 404). +%% ------------------------------ +%% webrtc +%% ------------------------------ + + +ws_webrtc(Sock, Request) -> + try + case qhl_ws:handshake(Request) of + {ok, Response} -> + fd_httpd_utils:respond(Sock, Response), + ws_webrtc_loop(Sock); + Error -> + tell("ws_webrtc: error: ~tp", [Error]), + fd_httpd_utils:http_err(Sock, 400) + end + catch + X:Y:Z -> + tell(error, "CRASH ws_webrtc: ~tp:~tp:~tp", [X, Y, Z]), + fd_httpd_utils:http_err(Sock, 500) + end. + +-record(rs, + {ident = username = undefined :: undefined | string(), + peers = undefined :: undefined | [ +-type webrtc_state() :: {username + +ws_webrtc_loop( + %% ------------------------------ %% echo %% ------------------------------