From 085507519aed799d955f9cfbce1ddf8047e946e0 Mon Sep 17 00:00:00 2001 From: Cody Larkin Date: Sat, 11 Apr 2026 18:42:29 -0400 Subject: [PATCH] updated to shared lib --- RelayClient/MainPage.xaml.cs | 29 ++++++++------- RelayClient/Resources/Raw/wwwroot/index.js | 8 ++--- RelayServer/Endpoints/RtcEndpoints.cs | 8 ++--- .../Services/Chat/ChatSocketBehavior.cs | 10 +++--- .../Services/Rtc/RtcNotificationService.cs | 18 ---------- RelayShared/Rtc/RtcModels.cs | 35 +++++++++++-------- 6 files changed, 46 insertions(+), 62 deletions(-) diff --git a/RelayClient/MainPage.xaml.cs b/RelayClient/MainPage.xaml.cs index 78bf794..94530a4 100644 --- a/RelayClient/MainPage.xaml.cs +++ b/RelayClient/MainPage.xaml.cs @@ -2,7 +2,6 @@ using WebSocketSharp; using System.Text.Json; using System.Text.Json.Serialization; -using System.Text.Json.Serialization.Metadata; using RelayShared.Rtc; namespace RelayClient; @@ -82,7 +81,7 @@ public partial class MainPage : ContentPage var payload = new SocketEncryptedMessage { ChannelId = _currentChannelId!, - Type = "client_encrypted_chat", + Type = SignalType.ClientEncryptedChat, SenderUsername = _username, CipherText = encrypted.CipherText, Nonce = encrypted.Nonce, @@ -119,9 +118,9 @@ public partial class MainPage : ContentPage if (!root.TryGetProperty("Type", out var typeElement)) return; - var type = typeElement.GetString(); + var type = (SignalType) typeElement.GetInt32(); - if (type == "channel_list") + if (type == SignalType.ChannelList) { var channelList = JsonSerializer.Deserialize(e.Data); if (channelList is null) @@ -154,7 +153,7 @@ public partial class MainPage : ContentPage return; } - if (type == "server_public_key") + if (type == SignalType.ServerPublicKey) { var serverKeyMessage = JsonSerializer.Deserialize(e.Data); if (serverKeyMessage is not null) @@ -166,7 +165,7 @@ public partial class MainPage : ContentPage return; } - if (type == "encrypted_rtc_signal") + if (type == SignalType.EncryptedSignal) { var payload = JsonSerializer.Deserialize(e.Data); if (payload is null) @@ -199,13 +198,13 @@ 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 == SignalType.OfferUpdated || type == SignalType.AnswerUpdated || type == SignalType.CandidateAdded || type == SignalType.CallLeft) { var rtcNotification = JsonSerializer.Deserialize(e.Data); if (rtcNotification is null) return; - var notificationType = rtcNotification.Type ?? string.Empty; + var notificationType = rtcNotification.Type ?? null; var notificationChannelId = rtcNotification.ChannelId ?? string.Empty; if (notificationChannelId != _currentChannelId) @@ -217,13 +216,13 @@ public partial class MainPage : ContentPage { switch (notificationType) { - case "rtc_offer_updated": + case SignalType.OfferUpdated : { var offer = await GetRtcOffer(); await SendRtcSignalToJsAsync(offer); break; } - case "rtc_answer_updated": + case SignalType.AnswerUpdated: { var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId); if (answer is not null) @@ -232,7 +231,7 @@ public partial class MainPage : ContentPage } break; } - case "rtc_candidate_added": + case SignalType.CandidateAdded: { try { @@ -246,7 +245,7 @@ public partial class MainPage : ContentPage break; } - case "rtc_call_left": + case SignalType.CallLeft: { SafeSendRawToWebView("RTC call left notification received."); RtcLeaveCallback(); @@ -258,7 +257,7 @@ public partial class MainPage : ContentPage return; } - if (type != "encrypted_chat") + if (type != SignalType.EncryptedChat) return; var pyload = JsonSerializer.Deserialize(e.Data); @@ -429,7 +428,7 @@ public partial class MainPage : ContentPage bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId); - // SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}"); + SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}"); return active; } @@ -588,7 +587,7 @@ public partial class MainPage : ContentPage var payload = new SocketRtcSignalMessage { - Type = "encrypted_rtc_signal", + Type = SignalType.EncryptedSignal, SenderUsername = _username, ChannelId = rtcSignal.ChannelId, CipherText = encrypted.CipherText, diff --git a/RelayClient/Resources/Raw/wwwroot/index.js b/RelayClient/Resources/Raw/wwwroot/index.js index 9a97549..8933478 100644 --- a/RelayClient/Resources/Raw/wwwroot/index.js +++ b/RelayClient/Resources/Raw/wwwroot/index.js @@ -261,7 +261,7 @@ async function ensurePeerConnection2() peerConnection.onicecandidate = async (event) => { console.log(`Ice Candidate: ${JSON.stringify(event.candidate)}`); - LogMessage(`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); }; @@ -312,8 +312,6 @@ async function channelCallJoin(activeCall) // LogMessage("Joining call with media answer: " + JSON.stringify(answer)); // LogMessage("Calling C# WriteRtcAnswer with: " + JSON.stringify(answer)); await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(answer)]); - LogMessage("C# WriteRtcAnswer invoked"); - //TODO: Update offer in SurrealDB to include answer } else { @@ -345,10 +343,10 @@ async function IceCandidateAdded(candidate) { if (peerConnection.currentRemoteDescription) { await peerConnection.addIceCandidate(candidate); - LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate)); + // LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate)); } else { - LogMessage("RemoteDescription Missing") + // LogMessage("RemoteDescription Missing") candidateQueue.push(candidate); } diff --git a/RelayServer/Endpoints/RtcEndpoints.cs b/RelayServer/Endpoints/RtcEndpoints.cs index 2fc9c35..04a1673 100644 --- a/RelayServer/Endpoints/RtcEndpoints.cs +++ b/RelayServer/Endpoints/RtcEndpoints.cs @@ -20,7 +20,7 @@ public static class RtcEndpoints RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage { - Type = "rtc_offer_updated", + Type = SignalType.OfferUpdated, ChannelId = request.ChannelId, Username = request.Username }); @@ -58,7 +58,7 @@ public static class RtcEndpoints RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage { - Type = "rtc_answer_updated", + Type = SignalType.AnswerUpdated, ChannelId = request.ChannelId }); @@ -91,7 +91,7 @@ public static class RtcEndpoints RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage { - Type = "rtc_candidate_added", + Type = SignalType.CandidateAdded, ChannelId = request.ChannelId, Username = request.Username, Direction = JsonSerializer.Serialize(request.Candidate) @@ -124,7 +124,7 @@ public static class RtcEndpoints RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage { - Type = "rtc_call_left", + Type = SignalType.CallLeft, ChannelId = request.ChannelId, Username = request.Username }); diff --git a/RelayServer/Services/Chat/ChatSocketBehavior.cs b/RelayServer/Services/Chat/ChatSocketBehavior.cs index 9214a1e..c29821b 100644 --- a/RelayServer/Services/Chat/ChatSocketBehavior.cs +++ b/RelayServer/Services/Chat/ChatSocketBehavior.cs @@ -156,7 +156,7 @@ public class ChatSocketBehavior : WebSocketBehavior var payload = new SocketChannelList { - Type = "channel_list", + Type = SignalType.ChannelList, Channels = channels }; @@ -176,7 +176,7 @@ public class ChatSocketBehavior : WebSocketBehavior var payload = new ServerPublicKeyMessage { - Type = "server_public_key", + Type = SignalType.ServerPublicKey, PublicKey = ServerPublicKey }; @@ -202,7 +202,7 @@ public class ChatSocketBehavior : WebSocketBehavior return; } - if (clientPayload is null || clientPayload.Type != "client_encrypted_chat") + if (clientPayload is null || clientPayload.Type != SignalType.ClientEncryptedChat) return; if (!EnsureCoreReady() || !EnsureCryptoReady()) @@ -262,7 +262,7 @@ public class ChatSocketBehavior : WebSocketBehavior var outbound = new SocketEncryptedMessage { - Type = "encrypted_chat", + Type = SignalType.EncryptedChat, SenderUsername = clientPayload.SenderUsername, RecipientUsername = client.Username, ChannelId = clientPayload.ChannelId, @@ -340,7 +340,7 @@ public class ChatSocketBehavior : WebSocketBehavior var outbound = new SocketEncryptedMessage { - Type = "encrypted_chat", + Type = SignalType.EncryptedChat, SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId), RecipientUsername = username, ChannelId = channelId, diff --git a/RelayServer/Services/Rtc/RtcNotificationService.cs b/RelayServer/Services/Rtc/RtcNotificationService.cs index 37533fd..544c6df 100644 --- a/RelayServer/Services/Rtc/RtcNotificationService.cs +++ b/RelayServer/Services/Rtc/RtcNotificationService.cs @@ -25,22 +25,4 @@ 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); - } - } } \ No newline at end of file diff --git a/RelayShared/Rtc/RtcModels.cs b/RelayShared/Rtc/RtcModels.cs index 193d0a5..a3ad885 100644 --- a/RelayShared/Rtc/RtcModels.cs +++ b/RelayShared/Rtc/RtcModels.cs @@ -2,20 +2,25 @@ namespace RelayShared.Rtc; -public static class RtcSignalTypes +public enum SignalType { - public const string Offer = "rtc_offer"; - public const string Answer = "rtc_answer"; - public const string Candidate = "rtc_candidate"; - public const string OfferUpdated = "rtc_offer_updated"; - public const string AnswerUpdated = "rtc_answer_updated"; - public const string CandidateAdded = "rtc_candidate_added"; - public const string CallLeft = "rtc_call_left"; + Offer, + Answer, + Candidate, + OfferUpdated, + AnswerUpdated, + CandidateAdded, + CallLeft, + ChannelList, + ServerPublicKey, + EncryptedSignal, + EncryptedChat, + ClientEncryptedChat } public sealed class RtcSignalMessage { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } public string From { get; set; } = string.Empty; public string ChannelId { get; set; } = string.Empty; public string? Sdp { get; set; } @@ -27,7 +32,7 @@ public sealed class RtcSignalMessage public sealed class RtcNotificationMessage { - public string? Type { get; set; } + public SignalType? Type { get; set; } public string? ChannelId { get; set; } public string? Username { get; set; } public string? Direction { get; set; } @@ -35,13 +40,13 @@ public sealed class RtcNotificationMessage public sealed class ServerPublicKeyMessage { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } = SignalType.ServerPublicKey; public string PublicKey { get; set; } = string.Empty; } public sealed class SocketRtcSignalMessage { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } public string SenderUsername { get; set; } = string.Empty; public string ChannelId { get; set; } = string.Empty; public string CipherText { get; set; } = string.Empty; @@ -52,7 +57,7 @@ public sealed class SocketRtcSignalMessage public sealed class SocketEncryptedMessage { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } = SignalType.EncryptedChat; public string SenderUsername { get; set; } = string.Empty; public string RecipientUsername { get; set; } = string.Empty; public string ChannelId { get; set; } = string.Empty; @@ -71,7 +76,7 @@ public sealed class ChannelItem public sealed class SocketChannelList { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } = SignalType.ChannelList; public List Channels { get; set; } = []; } @@ -98,7 +103,7 @@ public sealed class RtcLeaveRequest public sealed class RtcSessionDescription { - public string Type { get; set; } = string.Empty; + public SignalType Type { get; set; } public string Sdp { get; set; } = string.Empty; }