diff --git a/RelayClient/MainPage.xaml.cs b/RelayClient/MainPage.xaml.cs index e667ff9..ec89dcc 100644 --- a/RelayClient/MainPage.xaml.cs +++ b/RelayClient/MainPage.xaml.cs @@ -2,6 +2,8 @@ using RelayClient.Models; using WebSocketSharp; using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; namespace RelayClient; @@ -226,8 +228,7 @@ public partial class MainPage : ContentPage var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId); if (answer is not null) { - var json = JsonSerializer.Serialize(answer); - await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({json})"); + await AnswerCallback(answer); } break; } @@ -476,10 +477,22 @@ public partial class MainPage : ContentPage } } - public async void AnswerCallback(RtcDescription answer) + public async Task AnswerCallback(RtcDescription answer) { - var json = JsonSerializer.Serialize(answer); - await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({json})"); + string json = JsonSerializer.Serialize(answer); + SafeSendRawToWebView("WriteRtcAnswer answered with: " + json); + try + { + SafeSendRawToWebView("Pre"); + // await hybridWebView.InvokeJavaScriptAsync("CSharpCallTest", ["value from C#"], [HybridJSTypeString.Default.String]); + // SafeSendRawToWebView("Mid"); + await hybridWebView.InvokeJavaScriptAsync("AnswerCallback", [json], [HybridJSTypeString.Default.String]); + SafeSendRawToWebView("End"); + } + catch (Exception ex) + { + SafeSendRawToWebView("WriteRtcAnswer failed: " + ex.Message); + } } private void OnSendMessageButtonClicked(object sender, EventArgs e) @@ -568,4 +581,20 @@ public partial class MainPage : ContentPage } }); } + + [JsonSourceGenerationOptions(WriteIndented = true)] + [JsonSerializable(typeof(string))] + internal partial class HybridJSTypeString : JsonSerializerContext + { + // This type's attributes specify JSON serialization info to preserve type structure + // for trimmed builds. + } + [JsonSourceGenerationOptions(WriteIndented = true)] + [JsonSerializable(typeof(RtcDescription))] + internal partial class HybridJSTypeRtcDescription : JsonSerializerContext + { + // This type's attributes specify JSON serialization info to preserve type structure + // for trimmed builds. + } + } \ No newline at end of file diff --git a/RelayClient/Resources/Raw/wwwroot/index.js b/RelayClient/Resources/Raw/wwwroot/index.js index 806ff76..18c7760 100644 --- a/RelayClient/Resources/Raw/wwwroot/index.js +++ b/RelayClient/Resources/Raw/wwwroot/index.js @@ -258,13 +258,21 @@ async function channelCallJoin(activeCall) }); } } + +async function CSharpCallTest(value) +{ + LogMessage("Called from C#: " + value); +} async function AnswerCallback(answer) { - LogMessage("Answer: " + JSON.stringify(answer)); + LogMessage("Answer: " + answer); - if (!peerConnection.currentRemoteDescription && answer.answer) + let callBack = JSON.parse(answer); + LogMessage("Call Back: " + callBack); + + if (!peerConnection.currentRemoteDescription && callBack) { - LogMessage("Current answer: " + answer); + LogMessage("Current answer: " + callBack); const desc = new RTCSessionDescription(answer); await peerConnection.setRemoteDescription(desc); }