Fixed all underlying issues with the "Answer" call.

This commit is contained in:
2026-04-06 16:08:37 -04:00
parent aa7f6597c4
commit 7d8755ca71
6 changed files with 134 additions and 38 deletions

View File

@@ -197,6 +197,55 @@ public partial class MainPage : ContentPage
return;
}
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)
return;
var notificationType = rtcNotification.Type ?? string.Empty;
var notificationChannelId = rtcNotification.ChannelId ?? string.Empty;
if (notificationChannelId != _currentChannelId)
return;
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)
{
var json = JsonSerializer.Serialize(answer);
await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({json})");
}
break;
}
case "rtc_candidate_added":
{
break;
}
case "rtc_call_left":
{
SafeSendRawToWebView("RTC call left notification received.");
break;
}
}
});
return;
}
if (type != "encrypted_chat")
return;
@@ -358,15 +407,28 @@ public partial class MainPage : ContentPage
public async Task<bool> JoinRtcChannel()
{
//TODO: get bool value for if channel ID has an active call
//TODO: Join RTC using current channel ID
SafeSendRawToWebView($"Attempting to join RTC Channel {_currentChannelName}");
bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId);
SafeSendRawToWebView($"Rtc Channel {_currentChannelName} is active: {active}");
return active;
//await hybridWebView.EvaluateJavaScriptAsync($"window.channelCallJoin({active})");
if (string.IsNullOrWhiteSpace(_currentChannelId))
return false;
_wsc.Send($"RTC_JOIN_CHANNEL|{_username}|{_currentChannelId}");
SafeSendRawToWebView($"Attempting to join RTC Channel {_currentChannelName} | {_currentChannelId} ");
bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId);
SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}");
return active;
}
public void LeaveRtcChannel()
{
if (string.IsNullOrWhiteSpace(_currentChannelId))
return;
_wsc.Send($"RTC_LEAVE_CHANNEL|{_username}|{_currentChannelId}");
}
public async void WriteRtcOffer(string json)
{
try
@@ -391,21 +453,33 @@ public partial class MainPage : ContentPage
RtcDescription? offer = await ServerAPI.GetOffersForChannelAsync(_currentChannelId);
return JsonSerializer.Serialize(offer);
}
public async void WriteRtcAnswer(string json)
{
RtcDescription? description = JsonSerializer.Deserialize<RtcDescription>(json);
DBOffer answer = new DBOffer
SafeSendRawToWebView("WriteRtcAnswer entered with: " + json);
try
{
ChannelId = _currentChannelId,
Username = _username,
SessionDescription = description
};
await ServerAPI.PostAnswerAsync(answer);
RtcDescription? description = JsonSerializer.Deserialize<RtcDescription>(json);
DBOffer answer = new DBOffer
{
ChannelId = _currentChannelId,
Username = _username,
SessionDescription = description
};
await ServerAPI.PostAnswerAsync(answer);
SafeSendRawToWebView("WriteRtcAnswer posted successfully");
}
catch (Exception ex)
{
SafeSendRawToWebView("WriteRtcAnswer failed: " + ex.Message);
}
}
public async void AnswerCallback(RtcDescription answer)
{
await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({answer})");
var json = JsonSerializer.Serialize(answer);
await hybridWebView.EvaluateJavaScriptAsync($"window.AnswerCallback({json})");
}
private void OnSendMessageButtonClicked(object sender, EventArgs e)