This commit is contained in:
Peter Harpending 2026-02-09 14:48:47 -08:00
parent cc95fc5829
commit b6436c84ee
3 changed files with 91 additions and 6 deletions

View File

@ -13,8 +13,61 @@
main(); main();
async function async function
main() main
()
: Promise<void>
{ {
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<void>
{
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);
}

View File

@ -16,13 +16,16 @@
<h1 class="content-title">FEWD: webrtc demo</h1> <h1 class="content-title">FEWD: webrtc demo</h1>
<div id="init"> <div id="init">
<label for="initName">username:</label> <label for="init-name">username:</label>
<input autofocus id="initName" type="text"></input> <input autofocus id="init-name" type="text" value='alice'>
<button id="initSubmit">Join</button> <button id="init-join">connect</button>
</div> </div>
<div id="peers" hidden> <div id="peers" hidden>
Peers <h1>Peers</h1>
<ul id="peers-ul"></ul>
</div> </div>
</div> </div>

View File

@ -233,6 +233,7 @@ handle_request(Sock, R = #request{method = M, path = P}, Received) when M =/= un
route(Sock, get, Route, Request, Received) -> route(Sock, get, Route, Request, Received) ->
case Route of case Route of
<<"/ws/echo">> -> ws_echo(Sock, Request) , Received; <<"/ws/echo">> -> ws_echo(Sock, Request) , Received;
<<"/ws/webrtc">> -> ws_webrtc(Sock, Request) , Received;
<<"/">> -> route_static(Sock, <<"/index.html">>) , Received; <<"/">> -> route_static(Sock, <<"/index.html">>) , Received;
_ -> route_static(Sock, Route) , Received _ -> route_static(Sock, Route) , Received
end; end;
@ -279,6 +280,34 @@ respond_static(Sock, not_found) ->
fd_httpd_utils:http_err(Sock, 404). 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 %% echo
%% ------------------------------ %% ------------------------------