updated to shared lib

This commit is contained in:
2026-04-11 18:42:29 -04:00
parent a67f94b08e
commit 085507519a
6 changed files with 46 additions and 62 deletions

View File

@@ -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<SocketChannelList>(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<ServerPublicKeyMessage>(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<SocketRtcSignalMessage>(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<RtcNotificationMessage>(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<SocketEncryptedMessage>(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,

View File

@@ -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);
}

View File

@@ -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
});

View File

@@ -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,

View File

@@ -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);
}
}
}

View File

@@ -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<ChannelItem> 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;
}