From 38662f66554deea36c6ef20b00cd21e4657db2e9 Mon Sep 17 00:00:00 2001 From: Cody Larkin Date: Wed, 29 Apr 2026 13:52:35 -0400 Subject: [PATCH] Fix attempts for RTC calls --- RelayClient/Resources/Raw/wwwroot/index.js | 10 ++++++- RelayClient/Resources/Raw/wwwroot/rtc.js | 8 +++--- RelayClient/Services/RtcBridgeService.cs | 31 +++++++++++++++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/RelayClient/Resources/Raw/wwwroot/index.js b/RelayClient/Resources/Raw/wwwroot/index.js index 2d822bb..58ec2d3 100644 --- a/RelayClient/Resources/Raw/wwwroot/index.js +++ b/RelayClient/Resources/Raw/wwwroot/index.js @@ -49,4 +49,12 @@ window.addEventListener("load", async () => { Media.wireDeviceSelectors(); await Media.loadDevices(); await Media.ensureLocalMedia(); -}); \ No newline at end of file +}); + +function testIndex(rawJson) +{ + const data = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson; + if (data.type === "rtc_offer") + HandleOffer(rawJson) + LogMessage("Called by SendRtcSignalToJsAsync from C#!") +} \ No newline at end of file diff --git a/RelayClient/Resources/Raw/wwwroot/rtc.js b/RelayClient/Resources/Raw/wwwroot/rtc.js index 1d1476e..81e38ad 100644 --- a/RelayClient/Resources/Raw/wwwroot/rtc.js +++ b/RelayClient/Resources/Raw/wwwroot/rtc.js @@ -1,4 +1,4 @@ -const peerConnections = {}; +const peerConnections = {}; async function joinChannelCall() { LogMessage("Current username: " + currentUsername); @@ -24,7 +24,7 @@ async function joinChannelCall() { } for (const username of existingUsers) { - await sendOffer(username); + await sendOffer(username); //Creates an offer to each person in call for MESH RTC } } @@ -34,6 +34,7 @@ async function sendOffer(username) { await Media.applyLocalStreamToPeerConnection(pc, username); const offer = await pc.createOffer(); + // LogMessage(`Offer created: ${JSON.stringify(offer)}`); await pc.setLocalDescription(offer); await RelaySocket.sendRtcSignal({ @@ -88,11 +89,12 @@ async function handleRtcSignal(rawJson) { } async function handleOffer(msg) { + LogMessage(`Offer handler: ${msg}`); const pc = await ensurePeerConnectionForUser(msg.from); await Media.ensureLocalMedia(); await Media.applyLocalStreamToPeerConnection(pc, msg.from); - + // const offer = JSON.parse(msg.offer); await pc.setRemoteDescription({ type: "offer", sdp: msg.sdp diff --git a/RelayClient/Services/RtcBridgeService.cs b/RelayClient/Services/RtcBridgeService.cs index 83dacb0..db08c48 100644 --- a/RelayClient/Services/RtcBridgeService.cs +++ b/RelayClient/Services/RtcBridgeService.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; using RelayClient.Crypto; using RelayShared.Rtc; using RelayShared.Services; @@ -70,6 +71,7 @@ public sealed class RtcBridgeService rtcSignal.ChannelId ??= _getCurrentChannelId(); rtcSignal.From ??= _username; + // _sendRawToWebView($"RTC_SIGNAL file: {JsonSerializer.Serialize(rtcSignal)}"); if (string.IsNullOrWhiteSpace(rtcSignal.ChannelId)) { _sendRawToWebView("SendRtcSignal failed: missing channel id."); @@ -116,13 +118,20 @@ public sealed class RtcBridgeService public async Task HandleIncomingRtcSignalAsync(SocketRtcSignalMessage payload) { + _sendRawToWebView("HandleIncomingRtcSignal called"); var currentChannelId = _getCurrentChannelId(); if (payload.ChannelId != currentChannelId) + { + _sendRawToWebView("Channel id does not match"); return; + } if (payload.SenderUsername == _username) + { + _sendRawToWebView("Received own message"); return; + } string decryptedJson; @@ -152,6 +161,7 @@ public sealed class RtcBridgeService try { rtcSignal = JsonSerializer.Deserialize(decryptedJson); + // _sendRawToWebView($"Received Encrypted Signal: [{rtcSignal.From}]: {rtcSignal.Offer}"); } catch (Exception ex) { @@ -169,9 +179,9 @@ public sealed class RtcBridgeService return; } - _sendRawToWebView("Received encrypted RTC signal: " + decryptedJson); + // _sendRawToWebView("Received encrypted RTC signal: " + decryptedJson); - await SendRtcSignalToJsAsync(decryptedJson); + await SendRtcSignalToJsAsync(rtcSignal); } public Task PushRtcContextToJsAsync() @@ -188,13 +198,15 @@ public sealed class RtcBridgeService return Task.CompletedTask; } - private Task SendRtcSignalToJsAsync(string rawJson) + private Task SendRtcSignalToJsAsync(RtcSignalMessage data) { MainThread.BeginInvokeOnMainThread(async () => { try { - var jsArg = JsonSerializer.Serialize(rawJson); + + await _hybridWebView.InvokeJavaScriptAsync("testIndex", [data], [RtcJsType.Default.RtcSignalMessage]); + var jsArg = JsonSerializer.Serialize(data); await _hybridWebView.EvaluateJavaScriptAsync($@" try {{ @@ -221,4 +233,15 @@ public sealed class RtcBridgeService return Task.CompletedTask; } +} + +[JsonSourceGenerationOptions(WriteIndented = false)] +[JsonSerializable(typeof(RtcDescription))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(RtcSignalMessage))] +[JsonSerializable(typeof(IceCandidate))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(string))] +internal partial class RtcJsType : JsonSerializerContext +{ } \ No newline at end of file