It's still broken, but I made a bit of progress.
This commit is contained in:
@@ -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 == "rtc_offer_updated" || type == "rtc_answer_updated" || type == "rtc_candidate_added" || type == "rtc_call_left")
|
||||
|
||||
if (type is "rtc_offer_updated" or "rtc_answer_updated" or "rtc_candidate_added" or "rtc_call_left")
|
||||
{
|
||||
var rtcNotification = JsonSerializer.Deserialize<RtcNotificationMessage>(e.Data);
|
||||
if (rtcNotification is null)
|
||||
@@ -213,40 +213,10 @@ public partial class MainPage : ContentPage
|
||||
|
||||
SafeSendRawToWebView("RTC notification received: " + notificationType + " for " + notificationChannelId);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
_ = MainThread.InvokeOnMainThreadAsync(() => HandleRtcNotificationAsync(notificationType));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (type != "encrypted_chat")
|
||||
return;
|
||||
|
||||
@@ -477,23 +447,22 @@ public partial class MainPage : ContentPage
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
//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);
|
||||
// }
|
||||
//}
|
||||
|
||||
private void OnSendMessageButtonClicked(object sender, EventArgs e)
|
||||
{
|
||||
@@ -508,7 +477,8 @@ public partial class MainPage : ContentPage
|
||||
return;
|
||||
}
|
||||
|
||||
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
|
||||
Console.WriteLine($"[{_username}] JS RAW -> C#: {e.Message}");
|
||||
SafeSendRawToWebView($"JS RAW -> C#: {e.Message}");
|
||||
}
|
||||
|
||||
public void SendRtcSignal(string json)
|
||||
@@ -552,8 +522,12 @@ public partial class MainPage : ContentPage
|
||||
|
||||
private async Task SendRtcSignalToJsAsync(string rawJson)
|
||||
{
|
||||
SafeSendRawToWebView("Before Evaluate dispatchRtcSignal");
|
||||
|
||||
var jsArg = JsonSerializer.Serialize(rawJson);
|
||||
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
|
||||
await hybridWebView.EvaluateJavaScriptAsync($"window.dispatchRtcSignal({jsArg})");
|
||||
|
||||
SafeSendRawToWebView("After Evaluate dispatchRtcSignal");
|
||||
}
|
||||
|
||||
private async Task PushRtcContextToJsAsync()
|
||||
@@ -597,4 +571,65 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user