bare basic WS setup

This commit is contained in:
2026-03-20 23:59:08 -04:00
parent 4961ced384
commit 2b2b16271b
5 changed files with 36 additions and 12 deletions

View File

@@ -16,24 +16,27 @@ public partial class MainPage : ContentPage
private void SendButton_OnClicked(object? sender, EventArgs e)
{
SendMessage();
SendMessage(sender, e);
}
private void MessageEntry_OnCompleted(object? sender, EventArgs e)
{
SendMessage();
SendMessage(sender, e);
}
private void SendMessage()
private void SendMessage(object? sender, EventArgs e)
{
var text = MessageEntry.Text?.Trim();
if (string.IsNullOrWhiteSpace(text))
return;
MauiProgram.wsc.Send($"{_username}:{text}");
ChatSimulator.Send(_username, text);
// ChatSimulator.Send(_username, text);
Console.WriteLine($"[{_username}] sent message: {text}");
MessageEntry.Text = string.Empty;
MessageEntry.Focus();

View File

@@ -1,22 +1,26 @@
using Microsoft.Extensions.Logging;
using Microsoft.Maui.Hosting;
using WebSocketSharp;
namespace RelayClient;
public static class MauiProgram
{
// public static event Action<ChatMessage>? MessageSent;
public static WebSocket wsc = new WebSocket("ws://localhost:1337");
public static MauiApp CreateMauiApp()
{
wsc.OnMessage += (sender, e) => OnWebSocketRecieved(sender, e);
wsc.Connect();
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
builder.UseMauiApp<App>().ConfigureFonts(fonts =>
{
fonts.AddFont("AnonymousPro-Bold.ttf", "AnonymousProBold");
fonts.AddFont("AnonymousPro-BoldItalic.ttf", "AnonymousProBoldItalic");
fonts.AddFont("AnonymousPro-Italic.ttf", "AnonymousProItalic");
fonts.AddFont("AnonymousPro-Regular.ttf", "AnonymousProRegular");
});
#if DEBUG
builder.Logging.AddDebug();
@@ -24,4 +28,20 @@ public static class MauiProgram
return builder.Build();
}
public static void OnWebSocketRecieved(object? sender, MessageEventArgs e)
{
Console.WriteLine(sender.ToString());
ChatSimulator.Send(e.Data.Split(":")[0], e.Data.Split(":")[1]);
// var message = new ChatMessage
// {
// SenderUsername = e.Data.Split(":")[0],
// Text = e.Data.Split(":")[1],
// Timestamp = DateTime.Now
// };
//
// MessageSent?.Invoke(message);
}
}

View File

@@ -42,6 +42,7 @@
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
<PackageReference Include="SurrealDb.Net" Version="0.9.0" />
<PackageReference Include="WebSocketSharp" Version="1.0.3-rc11" />
</ItemGroup>
</Project>