Multi Client Support - Needs Update Post Socket to support Multiple "Clients" not in a single Bus.

This commit is contained in:
2026-03-20 22:48:04 -04:00
parent 44fa9a8bb2
commit 2dfc898e8a
4 changed files with 164 additions and 43 deletions

View File

@@ -1,9 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
namespace RelayClient;
namespace RelayClient;
public partial class App : Application
{
private bool _openedSecondWindow;
public App()
{
InitializeComponent();
@@ -11,6 +11,28 @@ public partial class App : Application
protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new AppShell());
var keeperWindow = new Window(new MainPage("Keeper317"))
{
Title = "Relay Client - Keeper317"
};
keeperWindow.Created += KeeperWindow_Created;
return keeperWindow;
}
private void KeeperWindow_Created(object? sender, EventArgs e)
{
if (_openedSecondWindow)
return;
_openedSecondWindow = false;
var kiraWindow = new Window(new MainPage("Ru_Kira"))
{
Title = "Relay Client - Ru_Kira"
};
Current?.OpenWindow(kiraWindow);
}
}

View File

@@ -0,0 +1,25 @@
namespace RelayClient;
public static class ChatSimulator
{
public static event Action<ChatMessage>? MessageSent;
public static void Send(string senderUsername, string text)
{
var message = new ChatMessage
{
SenderUsername = senderUsername,
Text = text,
Timestamp = DateTime.Now
};
MessageSent?.Invoke(message);
}
}
public sealed class ChatMessage
{
public required string SenderUsername { get; set; }
public required string Text { get; set; }
public required DateTime Timestamp { get; set; }
}

View File

@@ -1,36 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RelayClient.MainPage">
<ContentPage
x:Class="RelayClient.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="Relay Client">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a submarine number ten" />
<Grid RowDefinitions="Auto,*,Auto" Padding="12" RowSpacing="10">
<Label
Text="Hello, World!"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
<Border Grid.Row="0" StrokeThickness="1" Padding="10">
<VerticalStackLayout Spacing="4">
<Label x:Name="UserLabel"
Text="Logged in as: Unknown"
FontAttributes="Bold"
FontSize="18" />
<Label Text="#general"
FontSize="14" />
</VerticalStackLayout>
</Border>
<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
<Border Grid.Row="1" StrokeThickness="1" Padding="10">
<ScrollView x:Name="MessagesScrollView">
<VerticalStackLayout x:Name="MessagesLayout" Spacing="8" />
</ScrollView>
</Border>
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>
<Grid Grid.Row="2" ColumnDefinitions="*,Auto" ColumnSpacing="10">
<Entry x:Name="MessageEntry"
Grid.Column="0"
Placeholder="Type a message..."
ReturnType="Send"
Completed="MessageEntry_OnCompleted" />
<Button Grid.Column="1"
Text="Send"
Clicked="SendButton_OnClicked" />
</Grid>
</Grid>
</ContentPage>

View File

@@ -2,22 +2,92 @@
public partial class MainPage : ContentPage
{
int count = 0;
private readonly string _username;
public MainPage()
public MainPage(string username)
{
InitializeComponent();
_username = username;
UserLabel.Text = $"Logged in as: {_username}";
ChatSimulator.MessageSent += OnMessageSent;
}
private void OnCounterClicked(object? sender, EventArgs e)
private void SendButton_OnClicked(object? sender, EventArgs e)
{
count++;
SendMessage();
}
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
private void MessageEntry_OnCompleted(object? sender, EventArgs e)
{
SendMessage();
}
SemanticScreenReader.Announce(CounterBtn.Text);
private void SendMessage()
{
var text = MessageEntry.Text?.Trim();
if (string.IsNullOrWhiteSpace(text))
return;
ChatSimulator.Send(_username, text);
Console.WriteLine($"[{_username}] sent message: {text}");
MessageEntry.Text = string.Empty;
MessageEntry.Focus();
}
private void OnMessageSent(ChatMessage message)
{
MainThread.BeginInvokeOnMainThread(async () =>
{
bool isOwnMessage = message.SenderUsername == _username;
var bubble = new Border
{
StrokeThickness = 1,
Padding = 10,
Margin = isOwnMessage
? new Thickness(40, 0, 0, 0)
: new Thickness(0, 0, 40, 0),
HorizontalOptions = isOwnMessage
? LayoutOptions.End
: LayoutOptions.Start,
Content = new VerticalStackLayout
{
Spacing = 2,
Children =
{
new Label
{
Text = message.SenderUsername,
FontAttributes = FontAttributes.Bold,
FontSize = 12
},
new Label
{
Text = message.Text,
FontSize = 14
},
new Label
{
Text = message.Timestamp.ToString("h:mm tt"),
FontSize = 10
}
}
}
};
MessagesLayout.Children.Add(bubble);
await MessagesScrollView.ScrollToAsync(MessagesLayout, ScrollToPosition.End, true);
});
}
protected override void OnDisappearing()
{
ChatSimulator.MessageSent -= OnMessageSent;
base.OnDisappearing();
}
}