Merge branch 'refs/heads/main' into Shared-Files

This commit is contained in:
2026-04-10 14:13:01 -04:00
7 changed files with 115 additions and 45 deletions

View File

@@ -235,11 +235,22 @@ public partial class MainPage : ContentPage
}
case "rtc_candidate_added":
{
try
{
IceCandidate? iceCandidate = JsonSerializer.Deserialize<IceCandidate>(rtcNotification.Direction);
IceCandidateCallback(iceCandidate);
}
catch (Exception ex)
{
SafeSendRawToWebView($"Candidate rejected: {ex.Message}");
}
break;
}
case "rtc_call_left":
{
SafeSendRawToWebView("RTC call left notification received.");
RtcLeaveCallback();
break;
}
}
@@ -255,7 +266,7 @@ public partial class MainPage : ContentPage
if (pyload is null)
return;
if (pyload.RecipientUsername != _username)
if (pyload.RecipientUsername == _username)
return;
Console.WriteLine($"[{_username}] received encrypted payload for {pyload.RecipientUsername}");
@@ -407,6 +418,7 @@ public partial class MainPage : ContentPage
}
}
#region RTC Functions
public async Task<bool> JoinRtcChannel()
{
if (string.IsNullOrWhiteSpace(_currentChannelId))
@@ -489,6 +501,7 @@ public partial class MainPage : ContentPage
Username = _username,
Candidate = candidate
};
if (candidate == null) return;
await ServerAPI.PostIceCandidateAsync(DBCandidate);
}
catch (Exception ex)
@@ -497,11 +510,11 @@ public partial class MainPage : ContentPage
}
}
public async void IceCandidateCallback(string json)
public async void IceCandidateCallback(IceCandidate candidate)
{
try
{
await hybridWebView.InvokeJavaScriptAsync("IceCandidateAdded");
await hybridWebView.InvokeJavaScriptAsync("IceCandidateAdded", [candidate], [HybridJSType.Default.IceCandidate]);
}
catch (Exception ex)
{
@@ -520,23 +533,36 @@ public partial class MainPage : ContentPage
SafeSendRawToWebView("AnswerCallback failed: " + ex.Message);
}
}
private void OnSendMessageButtonClicked(object sender, EventArgs e)
{
SafeSendRawToWebView($"Hello from C#!");
}
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
public async void RtcLeaveCallback()
{
if (e.Message == "rtc_page_ready")
try
{
await PushRtcContextToJsAsync();
return;
await hybridWebView.InvokeJavaScriptAsync("RtcLeaveCall", [], []);
}
catch (Exception ex)
{
SafeSendRawToWebView("RtcLeaveCallback failed: " + ex.Message);
}
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
}
private async Task SendRtcSignalToJsAsync(string rawJson)
{
var jsArg = JsonSerializer.Serialize(rawJson);
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
} //Remove?
private async Task PushRtcContextToJsAsync()
{
var usernameJson = JsonSerializer.Serialize(_username);
var channelIdJson = JsonSerializer.Serialize(_currentChannelId);
await hybridWebView.EvaluateJavaScriptAsync($"window.setUsername({usernameJson})");
await hybridWebView.EvaluateJavaScriptAsync($"window.setChannelId({channelIdJson})");
Console.WriteLine($"[{_username}] pushed RTC context into HybridWebView.");
} //Remove?
public void SendRtcSignal(string json)
{
if (string.IsNullOrWhiteSpace(_serverPublicKey))
@@ -574,23 +600,24 @@ public partial class MainPage : ContentPage
_wsc.Send(JsonSerializer.Serialize(payload));
Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.ChannelId}");
}
} //Remove?
private async Task SendRtcSignalToJsAsync(string rawJson)
{
var jsArg = JsonSerializer.Serialize(rawJson);
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
}
private async Task PushRtcContextToJsAsync()
#endregion
private void OnSendMessageButtonClicked(object sender, EventArgs e)
{
var usernameJson = JsonSerializer.Serialize(_username);
var channelIdJson = JsonSerializer.Serialize(_currentChannelId);
SafeSendRawToWebView($"Hello from C#!");
}
await hybridWebView.EvaluateJavaScriptAsync($"window.setUsername({usernameJson})");
await hybridWebView.EvaluateJavaScriptAsync($"window.setChannelId({channelIdJson})");
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
{
if (e.Message == "rtc_page_ready")
{
await PushRtcContextToJsAsync();
return;
}
Console.WriteLine($"[{_username}] pushed RTC context into HybridWebView.");
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
}
private void SafeSendRawToWebView(string message)
@@ -611,6 +638,8 @@ public partial class MainPage : ContentPage
[JsonSourceGenerationOptions(WriteIndented = false)]
[JsonSerializable(typeof(RtcDescription))]
[JsonSerializable(typeof(List<RtcSignalMessage>))]
[JsonSerializable(typeof(IceCandidate))]
[JsonSerializable(typeof(List<IceCandidate>))]
[JsonSerializable(typeof(string))]
internal partial class HybridJSType : JsonSerializerContext
{