diff --git a/RelayClient/MainPage.xaml.cs b/RelayClient/MainPage.xaml.cs index e4a9544..7d548af 100644 --- a/RelayClient/MainPage.xaml.cs +++ b/RelayClient/MainPage.xaml.cs @@ -136,10 +136,11 @@ public partial class MainPage : ContentPage _currentChannelId = defaultChannel.ChannelId; _currentChannelName = defaultChannel.Name; - MainThread.BeginInvokeOnMainThread(() => + MainThread.BeginInvokeOnMainThread(async () => { ChannelLabel.Text = $"#{_currentChannelName}"; RenderChannelList(); + await PushRtcContextToJsAsync(); }); _wsc.Send($"GET_HISTORY|{_username}|{_currentChannelId}"); @@ -166,7 +167,10 @@ public partial class MainPage : ContentPage if (payload is null) return; - if (payload.RecipientUsername != _username) + if (payload.ChannelId != _currentChannelId) + return; + + if (payload.SenderUsername == _username) return; var privateKey = KeyStorage.LoadPrivateKey(_username); @@ -189,7 +193,7 @@ public partial class MainPage : ContentPage return; } - + if (type != "encrypted_chat") return; @@ -267,6 +271,11 @@ public partial class MainPage : ContentPage { _currentChannelId = channel.ChannelId; _currentChannelName = channel.Name; + + MainThread.BeginInvokeOnMainThread(async () => + { + await PushRtcContextToJsAsync(); + }); ChannelLabel.Text = $"#{_currentChannelName}"; RenderCurrentChannelMessages(); @@ -353,7 +362,7 @@ public partial class MainPage : ContentPage { if (e.Message == "rtc_page_ready") { - await InitializeRtcPageAsync(); + await PushRtcContextToJsAsync(); return; } @@ -388,7 +397,7 @@ public partial class MainPage : ContentPage { Type = "encrypted_rtc_signal", SenderUsername = _username, - RecipientUsername = rtcSignal.To, + ChannelId = rtcSignal.ChannelId, CipherText = encrypted.CipherText, Nonce = encrypted.Nonce, Tag = encrypted.Tag, @@ -396,7 +405,7 @@ public partial class MainPage : ContentPage }; _wsc.Send(JsonSerializer.Serialize(payload)); - Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.To}"); + Console.WriteLine($"[{_username}] sent RTC signal: {rtcSignal.Type} -> {rtcSignal.ChannelId}"); } private async Task SendRtcSignalToJsAsync(string rawJson) @@ -405,10 +414,14 @@ public partial class MainPage : ContentPage await hybridWebView.EvaluateJavaScriptAsync($"window.handleRtcSignal({jsArg})"); } - private async Task InitializeRtcPageAsync() + private async Task PushRtcContextToJsAsync() { var usernameJson = JsonSerializer.Serialize(_username); + var channelIdJson = JsonSerializer.Serialize(_currentChannelId); + await hybridWebView.EvaluateJavaScriptAsync($"window.setUsername({usernameJson})"); - Console.WriteLine($"[{_username}] pushed username into HybridWebView."); + await hybridWebView.EvaluateJavaScriptAsync($"window.setChannelId({channelIdJson})"); + + Console.WriteLine($"[{_username}] pushed RTC context into HybridWebView."); } } \ No newline at end of file diff --git a/RelayClient/Models/RtcSignalMessage.cs b/RelayClient/Models/RtcSignalMessage.cs index 9f3113d..d33732b 100644 --- a/RelayClient/Models/RtcSignalMessage.cs +++ b/RelayClient/Models/RtcSignalMessage.cs @@ -2,9 +2,9 @@ public class RtcSignalMessage { - public required string Type { get; set; } // rtc_offer / rtc_answer / rtc_ice_candidate / rtc_call_request / rtc_call_accept / rtc_call_reject + public required string Type { get; set; } // rtc_join / rtc_offer / rtc_answer / rtc_ice_candidate / rtc_leave public required string From { get; set; } - public required string To { get; set; } + public required string ChannelId { get; set; } public string? Sdp { get; set; } public string? Candidate { get; set; } diff --git a/RelayClient/Models/SocketRtcSignalMessage.cs b/RelayClient/Models/SocketRtcSignalMessage.cs index e3553f8..3cab9ff 100644 --- a/RelayClient/Models/SocketRtcSignalMessage.cs +++ b/RelayClient/Models/SocketRtcSignalMessage.cs @@ -4,7 +4,7 @@ public class SocketRtcSignalMessage { public required string Type { get; set; } // encrypted_rtc_signal public required string SenderUsername { get; set; } - public required string RecipientUsername { get; set; } + public required string ChannelId { get; set; } public required string CipherText { get; set; } public required string Nonce { get; set; } diff --git a/RelayClient/Resources/Raw/wwwroot/index.html b/RelayClient/Resources/Raw/wwwroot/index.html index 624413a..dae6071 100644 --- a/RelayClient/Resources/Raw/wwwroot/index.html +++ b/RelayClient/Resources/Raw/wwwroot/index.html @@ -11,8 +11,8 @@