updated to shared lib
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
using WebSocketSharp;
|
using WebSocketSharp;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Json.Serialization.Metadata;
|
|
||||||
using RelayShared.Rtc;
|
using RelayShared.Rtc;
|
||||||
|
|
||||||
namespace RelayClient;
|
namespace RelayClient;
|
||||||
@@ -82,7 +81,7 @@ public partial class MainPage : ContentPage
|
|||||||
var payload = new SocketEncryptedMessage
|
var payload = new SocketEncryptedMessage
|
||||||
{
|
{
|
||||||
ChannelId = _currentChannelId!,
|
ChannelId = _currentChannelId!,
|
||||||
Type = "client_encrypted_chat",
|
Type = SignalType.ClientEncryptedChat,
|
||||||
SenderUsername = _username,
|
SenderUsername = _username,
|
||||||
CipherText = encrypted.CipherText,
|
CipherText = encrypted.CipherText,
|
||||||
Nonce = encrypted.Nonce,
|
Nonce = encrypted.Nonce,
|
||||||
@@ -119,9 +118,9 @@ public partial class MainPage : ContentPage
|
|||||||
if (!root.TryGetProperty("Type", out var typeElement))
|
if (!root.TryGetProperty("Type", out var typeElement))
|
||||||
return;
|
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);
|
var channelList = JsonSerializer.Deserialize<SocketChannelList>(e.Data);
|
||||||
if (channelList is null)
|
if (channelList is null)
|
||||||
@@ -154,7 +153,7 @@ public partial class MainPage : ContentPage
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == "server_public_key")
|
if (type == SignalType.ServerPublicKey)
|
||||||
{
|
{
|
||||||
var serverKeyMessage = JsonSerializer.Deserialize<ServerPublicKeyMessage>(e.Data);
|
var serverKeyMessage = JsonSerializer.Deserialize<ServerPublicKeyMessage>(e.Data);
|
||||||
if (serverKeyMessage is not null)
|
if (serverKeyMessage is not null)
|
||||||
@@ -166,7 +165,7 @@ public partial class MainPage : ContentPage
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == "encrypted_rtc_signal")
|
if (type == SignalType.EncryptedSignal)
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(e.Data);
|
var payload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(e.Data);
|
||||||
if (payload is null)
|
if (payload is null)
|
||||||
@@ -199,13 +198,13 @@ public partial class MainPage : ContentPage
|
|||||||
return;
|
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);
|
var rtcNotification = JsonSerializer.Deserialize<RtcNotificationMessage>(e.Data);
|
||||||
if (rtcNotification is null)
|
if (rtcNotification is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var notificationType = rtcNotification.Type ?? string.Empty;
|
var notificationType = rtcNotification.Type ?? null;
|
||||||
var notificationChannelId = rtcNotification.ChannelId ?? string.Empty;
|
var notificationChannelId = rtcNotification.ChannelId ?? string.Empty;
|
||||||
|
|
||||||
if (notificationChannelId != _currentChannelId)
|
if (notificationChannelId != _currentChannelId)
|
||||||
@@ -217,13 +216,13 @@ public partial class MainPage : ContentPage
|
|||||||
{
|
{
|
||||||
switch (notificationType)
|
switch (notificationType)
|
||||||
{
|
{
|
||||||
case "rtc_offer_updated":
|
case SignalType.OfferUpdated :
|
||||||
{
|
{
|
||||||
var offer = await GetRtcOffer();
|
var offer = await GetRtcOffer();
|
||||||
await SendRtcSignalToJsAsync(offer);
|
await SendRtcSignalToJsAsync(offer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "rtc_answer_updated":
|
case SignalType.AnswerUpdated:
|
||||||
{
|
{
|
||||||
var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId);
|
var answer = await ServerAPI.GetAnswerForChannelAsync(_currentChannelId);
|
||||||
if (answer is not null)
|
if (answer is not null)
|
||||||
@@ -232,7 +231,7 @@ public partial class MainPage : ContentPage
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "rtc_candidate_added":
|
case SignalType.CandidateAdded:
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -246,7 +245,7 @@ public partial class MainPage : ContentPage
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "rtc_call_left":
|
case SignalType.CallLeft:
|
||||||
{
|
{
|
||||||
SafeSendRawToWebView("RTC call left notification received.");
|
SafeSendRawToWebView("RTC call left notification received.");
|
||||||
RtcLeaveCallback();
|
RtcLeaveCallback();
|
||||||
@@ -258,7 +257,7 @@ public partial class MainPage : ContentPage
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != "encrypted_chat")
|
if (type != SignalType.EncryptedChat)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var pyload = JsonSerializer.Deserialize<SocketEncryptedMessage>(e.Data);
|
var pyload = JsonSerializer.Deserialize<SocketEncryptedMessage>(e.Data);
|
||||||
@@ -429,7 +428,7 @@ public partial class MainPage : ContentPage
|
|||||||
|
|
||||||
bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId);
|
bool active = await ServerAPI.GetIsChannelActiveAsync(_currentChannelId);
|
||||||
|
|
||||||
// SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}");
|
SafeSendRawToWebView($"Rtc Channel {_currentChannelName} | {_currentChannelId} is active: {active}");
|
||||||
|
|
||||||
return active;
|
return active;
|
||||||
}
|
}
|
||||||
@@ -588,7 +587,7 @@ public partial class MainPage : ContentPage
|
|||||||
|
|
||||||
var payload = new SocketRtcSignalMessage
|
var payload = new SocketRtcSignalMessage
|
||||||
{
|
{
|
||||||
Type = "encrypted_rtc_signal",
|
Type = SignalType.EncryptedSignal,
|
||||||
SenderUsername = _username,
|
SenderUsername = _username,
|
||||||
ChannelId = rtcSignal.ChannelId,
|
ChannelId = rtcSignal.ChannelId,
|
||||||
CipherText = encrypted.CipherText,
|
CipherText = encrypted.CipherText,
|
||||||
|
|||||||
@@ -261,7 +261,7 @@ async function ensurePeerConnection2()
|
|||||||
|
|
||||||
peerConnection.onicecandidate = async (event) => {
|
peerConnection.onicecandidate = async (event) => {
|
||||||
console.log(`Ice Candidate: ${JSON.stringify(event.candidate)}`);
|
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 window.HybridWebView.InvokeDotNet("WriteIceCandidate", [JSON.stringify(event.candidate)]);
|
||||||
await IceCandidateAdded(event.candidate);
|
await IceCandidateAdded(event.candidate);
|
||||||
};
|
};
|
||||||
@@ -312,8 +312,6 @@ async function channelCallJoin(activeCall)
|
|||||||
// LogMessage("Joining call with media answer: " + JSON.stringify(answer));
|
// LogMessage("Joining call with media answer: " + JSON.stringify(answer));
|
||||||
// LogMessage("Calling C# WriteRtcAnswer with: " + JSON.stringify(answer));
|
// LogMessage("Calling C# WriteRtcAnswer with: " + JSON.stringify(answer));
|
||||||
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(answer)]);
|
await window.HybridWebView.InvokeDotNet("WriteRtcAnswer", [JSON.stringify(answer)]);
|
||||||
LogMessage("C# WriteRtcAnswer invoked");
|
|
||||||
//TODO: Update offer in SurrealDB to include answer
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -345,10 +343,10 @@ async function IceCandidateAdded(candidate)
|
|||||||
{
|
{
|
||||||
if (peerConnection.currentRemoteDescription) {
|
if (peerConnection.currentRemoteDescription) {
|
||||||
await peerConnection.addIceCandidate(candidate);
|
await peerConnection.addIceCandidate(candidate);
|
||||||
LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate));
|
// LogMessage("ICE CANDIDATE ADDED: " + JSON.stringify(candidate));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LogMessage("RemoteDescription Missing")
|
// LogMessage("RemoteDescription Missing")
|
||||||
candidateQueue.push(candidate);
|
candidateQueue.push(candidate);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public static class RtcEndpoints
|
|||||||
|
|
||||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||||
{
|
{
|
||||||
Type = "rtc_offer_updated",
|
Type = SignalType.OfferUpdated,
|
||||||
ChannelId = request.ChannelId,
|
ChannelId = request.ChannelId,
|
||||||
Username = request.Username
|
Username = request.Username
|
||||||
});
|
});
|
||||||
@@ -58,7 +58,7 @@ public static class RtcEndpoints
|
|||||||
|
|
||||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||||
{
|
{
|
||||||
Type = "rtc_answer_updated",
|
Type = SignalType.AnswerUpdated,
|
||||||
ChannelId = request.ChannelId
|
ChannelId = request.ChannelId
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ public static class RtcEndpoints
|
|||||||
|
|
||||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||||
{
|
{
|
||||||
Type = "rtc_candidate_added",
|
Type = SignalType.CandidateAdded,
|
||||||
ChannelId = request.ChannelId,
|
ChannelId = request.ChannelId,
|
||||||
Username = request.Username,
|
Username = request.Username,
|
||||||
Direction = JsonSerializer.Serialize(request.Candidate)
|
Direction = JsonSerializer.Serialize(request.Candidate)
|
||||||
@@ -124,7 +124,7 @@ public static class RtcEndpoints
|
|||||||
|
|
||||||
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
RtcNotificationService.BroadcastToChannel(new RtcNotificationMessage
|
||||||
{
|
{
|
||||||
Type = "rtc_call_left",
|
Type = SignalType.CallLeft,
|
||||||
ChannelId = request.ChannelId,
|
ChannelId = request.ChannelId,
|
||||||
Username = request.Username
|
Username = request.Username
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
|
|
||||||
var payload = new SocketChannelList
|
var payload = new SocketChannelList
|
||||||
{
|
{
|
||||||
Type = "channel_list",
|
Type = SignalType.ChannelList,
|
||||||
Channels = channels
|
Channels = channels
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
|
|
||||||
var payload = new ServerPublicKeyMessage
|
var payload = new ServerPublicKeyMessage
|
||||||
{
|
{
|
||||||
Type = "server_public_key",
|
Type = SignalType.ServerPublicKey,
|
||||||
PublicKey = ServerPublicKey
|
PublicKey = ServerPublicKey
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -202,7 +202,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clientPayload is null || clientPayload.Type != "client_encrypted_chat")
|
if (clientPayload is null || clientPayload.Type != SignalType.ClientEncryptedChat)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!EnsureCoreReady() || !EnsureCryptoReady())
|
if (!EnsureCoreReady() || !EnsureCryptoReady())
|
||||||
@@ -262,7 +262,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
|
|
||||||
var outbound = new SocketEncryptedMessage
|
var outbound = new SocketEncryptedMessage
|
||||||
{
|
{
|
||||||
Type = "encrypted_chat",
|
Type = SignalType.EncryptedChat,
|
||||||
SenderUsername = clientPayload.SenderUsername,
|
SenderUsername = clientPayload.SenderUsername,
|
||||||
RecipientUsername = client.Username,
|
RecipientUsername = client.Username,
|
||||||
ChannelId = clientPayload.ChannelId,
|
ChannelId = clientPayload.ChannelId,
|
||||||
@@ -340,7 +340,7 @@ public class ChatSocketBehavior : WebSocketBehavior
|
|||||||
|
|
||||||
var outbound = new SocketEncryptedMessage
|
var outbound = new SocketEncryptedMessage
|
||||||
{
|
{
|
||||||
Type = "encrypted_chat",
|
Type = SignalType.EncryptedChat,
|
||||||
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
|
SenderUsername = ExtractUsernameFromUserId(dbMessage.SenderUserId),
|
||||||
RecipientUsername = username,
|
RecipientUsername = username,
|
||||||
ChannelId = channelId,
|
ChannelId = channelId,
|
||||||
|
|||||||
@@ -25,22 +25,4 @@ public static class RtcNotificationService
|
|||||||
host.Sessions.SendTo(json, sessionId);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,20 +2,25 @@
|
|||||||
|
|
||||||
namespace RelayShared.Rtc;
|
namespace RelayShared.Rtc;
|
||||||
|
|
||||||
public static class RtcSignalTypes
|
public enum SignalType
|
||||||
{
|
{
|
||||||
public const string Offer = "rtc_offer";
|
Offer,
|
||||||
public const string Answer = "rtc_answer";
|
Answer,
|
||||||
public const string Candidate = "rtc_candidate";
|
Candidate,
|
||||||
public const string OfferUpdated = "rtc_offer_updated";
|
OfferUpdated,
|
||||||
public const string AnswerUpdated = "rtc_answer_updated";
|
AnswerUpdated,
|
||||||
public const string CandidateAdded = "rtc_candidate_added";
|
CandidateAdded,
|
||||||
public const string CallLeft = "rtc_call_left";
|
CallLeft,
|
||||||
|
ChannelList,
|
||||||
|
ServerPublicKey,
|
||||||
|
EncryptedSignal,
|
||||||
|
EncryptedChat,
|
||||||
|
ClientEncryptedChat
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class RtcSignalMessage
|
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 From { get; set; } = string.Empty;
|
||||||
public string ChannelId { get; set; } = string.Empty;
|
public string ChannelId { get; set; } = string.Empty;
|
||||||
public string? Sdp { get; set; }
|
public string? Sdp { get; set; }
|
||||||
@@ -27,7 +32,7 @@ public sealed class RtcSignalMessage
|
|||||||
|
|
||||||
public sealed class RtcNotificationMessage
|
public sealed class RtcNotificationMessage
|
||||||
{
|
{
|
||||||
public string? Type { get; set; }
|
public SignalType? Type { get; set; }
|
||||||
public string? ChannelId { get; set; }
|
public string? ChannelId { get; set; }
|
||||||
public string? Username { get; set; }
|
public string? Username { get; set; }
|
||||||
public string? Direction { get; set; }
|
public string? Direction { get; set; }
|
||||||
@@ -35,13 +40,13 @@ public sealed class RtcNotificationMessage
|
|||||||
|
|
||||||
public sealed class ServerPublicKeyMessage
|
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 string PublicKey { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class SocketRtcSignalMessage
|
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 SenderUsername { get; set; } = string.Empty;
|
||||||
public string ChannelId { get; set; } = string.Empty;
|
public string ChannelId { get; set; } = string.Empty;
|
||||||
public string CipherText { get; set; } = string.Empty;
|
public string CipherText { get; set; } = string.Empty;
|
||||||
@@ -52,7 +57,7 @@ public sealed class SocketRtcSignalMessage
|
|||||||
|
|
||||||
public sealed class SocketEncryptedMessage
|
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 SenderUsername { get; set; } = string.Empty;
|
||||||
public string RecipientUsername { get; set; } = string.Empty;
|
public string RecipientUsername { get; set; } = string.Empty;
|
||||||
public string ChannelId { get; set; } = string.Empty;
|
public string ChannelId { get; set; } = string.Empty;
|
||||||
@@ -71,7 +76,7 @@ public sealed class ChannelItem
|
|||||||
|
|
||||||
public sealed class SocketChannelList
|
public sealed class SocketChannelList
|
||||||
{
|
{
|
||||||
public string Type { get; set; } = string.Empty;
|
public SignalType Type { get; set; } = SignalType.ChannelList;
|
||||||
public List<ChannelItem> Channels { get; set; } = [];
|
public List<ChannelItem> Channels { get; set; } = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +103,7 @@ public sealed class RtcLeaveRequest
|
|||||||
|
|
||||||
public sealed class RtcSessionDescription
|
public sealed class RtcSessionDescription
|
||||||
{
|
{
|
||||||
public string Type { get; set; } = string.Empty;
|
public SignalType Type { get; set; }
|
||||||
public string Sdp { get; set; } = string.Empty;
|
public string Sdp { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user