Revert "It's still broken, but I made a bit of progress."

This reverts commit 9b666ee109.
This commit is contained in:
2026-04-06 20:43:03 -04:00
parent 98a837cc8b
commit df438e265b
2 changed files with 86 additions and 152 deletions

View File

@@ -1,4 +1,4 @@
using RelayClient.Crypto;
using RelayClient.Crypto;
using RelayClient.Models;
using WebSocketSharp;
using System.Text.Json;
@@ -109,7 +109,7 @@ public partial class MainPage : ContentPage
SafeSendRawToWebView($"[{_username}] RAW WS DATA: {e.Data}");
//Console.WriteLine($"[{_username}] RAW WS DATA: {e.Data}");
Console.WriteLine($"[{_username}] RAW WS DATA: {e.Data}");
try
{
@@ -198,8 +198,8 @@ public partial class MainPage : ContentPage
return;
}
if (type is "rtc_offer_updated" or "rtc_answer_updated" or "rtc_candidate_added" or "rtc_call_left")
if (type == "rtc_offer_updated" || type == "rtc_answer_updated" || type == "rtc_candidate_added" || type == "rtc_call_left")
{
var rtcNotification = JsonSerializer.Deserialize<RtcNotificationMessage>(e.Data);
if (rtcNotification is null)
@@ -213,10 +213,40 @@ public partial class MainPage : ContentPage
SafeSendRawToWebView("RTC notification received: " + notificationType + " for " + notificationChannelId);
_ = MainThread.InvokeOnMainThreadAsync(() => HandleRtcNotificationAsync(notificationType));
MainThread.BeginInvokeOnMainThread(async () =>
{
switch (notificationType)
{
case "rtc_offer_updated":
{
var offer = await GetRtcOffer();
await SendRtcSignalToJsAsync(offer);
break;
}
case "rtc_answer_updated":
{
var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId);
if (answer is not null)
{
await AnswerCallback(answer);
}
break;
}
case "rtc_candidate_added":
{
break;
}
case "rtc_call_left":
{
SafeSendRawToWebView("RTC call left notification received.");
break;
}
}
});
return;
}
if (type != "encrypted_chat")
return;
@@ -447,22 +477,23 @@ public partial class MainPage : ContentPage
}
}
//public async Task AnswerCallback(RtcDescription answer)
//{
// string json = JsonSerializer.Serialize(answer);
// SafeSendRawToWebView("WriteRtcAnswer answered with: " + json);
// try
// {
// SafeSendRawToWebView("Pre Evaluate");
// await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({json})");
// SafeSendRawToWebView("Post Evaluate");
// }
// catch (Exception ex)
// {
// SafeSendRawToWebView("WriteRtcAnswer failed: " + ex.Message);
// }
//}
public async Task AnswerCallback(RtcDescription answer)
{
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)
{
@@ -477,8 +508,7 @@ public partial class MainPage : ContentPage
return;
}
Console.WriteLine($"[{_username}] JS RAW -> C#: {e.Message}");
SafeSendRawToWebView($"JS RAW -> C#: {e.Message}");
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
}
public void SendRtcSignal(string json)
@@ -522,12 +552,8 @@ public partial class MainPage : ContentPage
private async Task SendRtcSignalToJsAsync(string rawJson)
{
SafeSendRawToWebView("Before Evaluate dispatchRtcSignal");
var jsArg = JsonSerializer.Serialize(rawJson);
await hybridWebView.EvaluateJavaScriptAsync($"window.dispatchRtcSignal({jsArg})");
SafeSendRawToWebView("After Evaluate dispatchRtcSignal");
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
}
private async Task PushRtcContextToJsAsync()
@@ -571,65 +597,4 @@ public partial class MainPage : ContentPage
// for trimmed builds.
}
private async Task HandleRtcNotificationAsync(string notificationType)
{
try
{
switch (notificationType)
{
case "rtc_offer_updated":
{
var offer = await ServerAPI.GetOffersForChannelAsync(_currentChannelId);
if (offer is not null)
{
var signal = new
{
type = "rtc_offer",
from = "server",
channelId = _currentChannelId,
sdp = offer.sdp
};
SafeSendRawToWebView("Dispatching rtc_offer to JS");
await SendRtcSignalToJsAsync(JsonSerializer.Serialize(signal));
}
break;
}
case "rtc_answer_updated":
{
var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId);
if (answer is not null)
{
var signal = new
{
type = "rtc_answer",
from = "server",
channelId = _currentChannelId,
sdp = answer.sdp
};
SafeSendRawToWebView("Dispatching rtc_answer to JS");
await SendRtcSignalToJsAsync(JsonSerializer.Serialize(signal));
}
break;
}
case "rtc_candidate_added":
{
break;
}
case "rtc_call_left":
{
SafeSendRawToWebView("RTC call left notification received.");
break;
}
}
}
catch (Exception ex)
{
SafeSendRawToWebView("RTC notification handler failed: " + ex.Message);
}
}
}