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)
{
@@ -521,21 +534,34 @@ public partial class MainPage : ContentPage
}
}
private void OnSendMessageButtonClicked(object sender, EventArgs e)
public async void RtcLeaveCallback()
{
SafeSendRawToWebView($"Hello from C#!");
try
{
await hybridWebView.InvokeJavaScriptAsync("RtcLeaveCall", [], []);
}
catch (Exception ex)
{
SafeSendRawToWebView("RtcLeaveCallback failed: " + ex.Message);
}
}
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
private async Task SendRtcSignalToJsAsync(string rawJson)
{
if (e.Message == "rtc_page_ready")
{
await PushRtcContextToJsAsync();
return;
}
var jsArg = JsonSerializer.Serialize(rawJson);
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
} //Remove?
await DisplayAlertAsync("Raw Message Received", e.Message, "OK");
}
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)
{
@@ -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?
#endregion
private void OnSendMessageButtonClicked(object sender, EventArgs e)
{
SafeSendRawToWebView($"Hello from C#!");
}
private async Task SendRtcSignalToJsAsync(string rawJson)
private async void OnHybridWebViewRawMessageReceived(object sender, HybridWebViewRawMessageReceivedEventArgs e)
{
var jsArg = JsonSerializer.Serialize(rawJson);
await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})");
if (e.Message == "rtc_page_ready")
{
await PushRtcContextToJsAsync();
return;
}
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.");
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
{

View File

@@ -4,6 +4,7 @@ let currentUsername = null;
let currentChannelId = null;
let availableCameras = [];
let availableMics = [];
let candidateQueue = [];
const configuration = {
iceServers:[
{
@@ -24,7 +25,6 @@ window.setChannelId = function(channelId) {
currentChannelId = channelId;
LogMessage("Channel set to: " + currentChannelId);
};
let userMedia = getUserMedia()
function LogMessage(msg) {
const messageLog = document.getElementById("messageLog");
messageLog.value += '\r\n' + msg;
@@ -95,7 +95,7 @@ async function ensurePeerConnection() {
peerConnection.onicegatheringstatechange = () => {
LogMessage("ICE gathering state: " + peerConnection.iceGatheringState);
};
}
} //Remove?
async function ensureLocalMedia(forceReload = false) {
const localMediaStatus = document.getElementById("localMediaStatus");
const localVideoStatus = document.getElementById("localVideoStatus");
@@ -238,7 +238,7 @@ async function joinChannelCall() {
// } catch (err) {
// LogMessage("joinChannelCall failed: " + err);
// }
}
} //Combine with channelCallJoin
async function ensurePeerConnection2()
{
@@ -262,6 +262,7 @@ async function ensurePeerConnection2()
console.log(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
LogMessage(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
await window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(event.candidate)]);
await IceCandidateAdded(event.candidate);
};
peerConnection.ontrack = (event) => {
@@ -334,12 +335,26 @@ async function AnswerCallbackJS(answer)
LogMessage("Current answer: " + JSON.stringify(answer));
const desc = new RTCSessionDescription(answer);
await peerConnection.setRemoteDescription(desc);
for (const candidate of candidateQueue) {
await peerConnection.addIceCandidate(candidate);
}
}
}
async function IceCandidateAdded(candidate)
{
if (peerConnection.currentRemoteDescription) {
await peerConnection.addIceCandidate(candidate);
LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate));
}
else {
LogMessage("RemoteDescription Missing")
candidateQueue.push(candidate);
}
}
async function RtcLeaveCall()
{}
async function handleRtcSignal(rawJson) {
try {
const msg = typeof rawJson === "string" ? JSON.parse(rawJson) : rawJson;
@@ -354,7 +369,7 @@ async function handleRtcSignal(rawJson) {
const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
await waitForIceGatheringComplete(peerConnection);
// await waitForIceGatheringComplete(peerConnection);
const payload = {
type: "rtc_offer",
@@ -386,7 +401,7 @@ async function handleRtcSignal(rawJson) {
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
await waitForIceGatheringComplete(peerConnection);
// await waitForIceGatheringComplete(peerConnection);
const payload = {
type: "rtc_answer",
@@ -429,7 +444,7 @@ async function handleRtcSignal(rawJson) {
} catch (err) {
LogMessage("handleRtcSignal failed: " + err);
}
}
} //Remove?
async function loadDevices() {
try {
@@ -513,9 +528,9 @@ async function waitForIceGatheringComplete(pc) {
pc.addEventListener("icegatheringstatechange", checkState);
});
}
} //Remove?
window.handleRtcSignal = handleRtcSignal;
// window.handleRtcSignal = handleRtcSignal;
window.addEventListener("HybridWebViewMessageReceived", function (e) {
LogMessage("Raw message: " + e.detail.message);

View File

@@ -1,4 +1,5 @@
using RelayServer.Models.Rtc;
using System.Text.Json;
using RelayServer.Models.Rtc;
using RelayServer.Services.Rtc;
namespace RelayServer.Endpoints;
@@ -86,7 +87,6 @@ public static class RtcEndpoints
request.Candidate.candidate,
request.Candidate.sdpMid,
request.Candidate.sdpMLineIndex
// request.Candidate.direction
);
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
@@ -94,7 +94,7 @@ public static class RtcEndpoints
Type = "rtc_candidate_added",
ChannelId = request.ChannelId,
Username = request.Username,
/*Direction = request.Direction*/
Direction = JsonSerializer.Serialize(request.Candidate)
});
return Results.Ok();

View File

@@ -7,3 +7,11 @@ public sealed class RtcNotificationMessage
public string? Username { get; set; }
public string? Direction { get; set; }
}
public sealed class RtcIceNotificationMessage
{
public required string Type { get; set; }
public required string ChannelId { get; set; }
public string? Username { get; set; }
public required IceCandidate Candidate { get; set; }
}

View File

@@ -25,4 +25,22 @@ public static class RtcNotificationService
host.Sessions.SendTo(json, sessionId);
}
}
public static void BroadcastToChannel(RtcIceNotificationMessage message)
{
if (Server is null)
return;
var host = Server.WebSocketServices["/"];
if (host is null)
return;
var json = JsonSerializer.Serialize(message);
var sessionIds = RtcChannelPresenceService.GetSessionsInChannel(message.ChannelId);
foreach (var sessionId in sessionIds)
{
host.Sessions.SendTo(json, sessionId);
}
}
}

View File

@@ -75,8 +75,8 @@ $wtArgs = @(
"new-tab --title `"RelayCore`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$coreScript`"",
"new-tab --title `"RelayServer`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$serverScript`"",
"new-tab --title `"Keeper317`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$keeperScript`"",
# "new-tab --title `"Test`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$testScript`"",
"new-tab --title `"Ru_Kira`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$kiraScript`""
#"new-tab --title `"Test`" `"$ps`" -NoExit -ExecutionPolicy Bypass -File `"$testScript`""
) -join " ; "
Write-Host ""