diff --git a/UI/ChatSignalR/README.md b/UI/ChatSignalR/README.md index 8b70c9770..91112fcd2 100644 --- a/UI/ChatSignalR/README.md +++ b/UI/ChatSignalR/README.md @@ -1,8 +1,31 @@ # Chat SignalR App -Implements [SignalR library](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-3.1) for asynchronous notifications in an Uno Platform application. +Implements [ASP.NET Core SignalR](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction) for real-time notifications in an Uno Platform application. -To try out the sample, first launch an instance of the UnoChat.Service project which hosts the server that the chat clients will connect to. Next, simply launch any number of UnoChat projects which will serve as the chat clients. If you want to add chat clients that are hosted by system consoles, you can launch any number of them as well using the UnoChat.Client.Console project. +To try out the sample: + +1. Launch `UnoChat.Service` first (the SignalR server). +2. Launch one or more `UnoChat` clients (Uno Platform app). +3. Optionally launch one or more `UnoChat.Client.Console` clients. + +All clients connect to `https://localhost:7167/chatHub` by default. + +> [!NOTE] +> An Azure account is not required for this sample. It runs locally. + +## Quick start + +From `src/`: + +```bash +# Run the SignalR server +dotnet run --project UnoChat.Service + +# Run the console client (optional) +dotnet run --project UnoChat.Client.Console +``` + +Run the Uno app from your IDE using one of the platform launch profiles in `UnoChat`. ![ChatSignalR Image](doc/assets/chatSignalR.png) diff --git a/UI/ChatSignalR/src/UnoChat.Client.Console/Program.cs b/UI/ChatSignalR/src/UnoChat.Client.Console/Program.cs index 07e1555fb..a7745e2f8 100644 --- a/UI/ChatSignalR/src/UnoChat.Client.Console/Program.cs +++ b/UI/ChatSignalR/src/UnoChat.Client.Console/Program.cs @@ -18,7 +18,7 @@ static async Task Main(string[] args) Console.WriteLine($"Ok {name} one second, we're going to connect to the SignalR server..."); var connection = new HubConnectionBuilder() - .WithUrl("https://localhost:7167/ChatHub") + .WithUrl("https://localhost:7167/chatHub") .WithAutomaticReconnect() .Build(); diff --git a/UI/ChatSignalR/src/UnoChat.Service/Program.cs b/UI/ChatSignalR/src/UnoChat.Service/Program.cs index 69e6e5889..951a2fcf6 100644 --- a/UI/ChatSignalR/src/UnoChat.Service/Program.cs +++ b/UI/ChatSignalR/src/UnoChat.Service/Program.cs @@ -1,17 +1,28 @@ -namespace UnoChat.Service +using UnoChat.Service.Hubs; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddSignalR(); + +builder.Services.AddCors(options => { - public class Program + options.AddPolicy("CorsPolicy", policy => { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } -} \ No newline at end of file + policy + .AllowAnyHeader() + .AllowAnyMethod() + .SetIsOriginAllowed(_ => true) + .AllowCredentials(); + }); +}); + +var app = builder.Build(); + +app.UseCors("CorsPolicy"); + +app.MapGet("/", () => "UnoChat SignalR Service is running."); +app.MapHub("/chatHub"); + +app.Run(); + +public partial class Program { } \ No newline at end of file diff --git a/UI/ChatSignalR/src/UnoChat.Service/Startup.cs b/UI/ChatSignalR/src/UnoChat.Service/Startup.cs deleted file mode 100644 index ae63fd27c..000000000 --- a/UI/ChatSignalR/src/UnoChat.Service/Startup.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace UnoChat.Service -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddRazorPages(); - services.AddSignalR(); - - services.AddCors(o => o.AddPolicy( - "CorsPolicy", - builder => builder - .AllowAnyOrigin() - .AllowAnyMethod() - .AllowAnyHeader() - ) - ); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } - app.UseCors("CorsPolicy"); - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapRazorPages(); - endpoints.MapHub("/chathub"); - }); - } - } -} diff --git a/UI/ChatSignalR/src/UnoChat/Presentation/ViewModel.cs b/UI/ChatSignalR/src/UnoChat/Presentation/ViewModel.cs index 5e8fe992b..71d0c7e07 100644 --- a/UI/ChatSignalR/src/UnoChat/Presentation/ViewModel.cs +++ b/UI/ChatSignalR/src/UnoChat/Presentation/ViewModel.cs @@ -64,7 +64,7 @@ public ViewModel() _allMessages = new ObservableCollection(); _connection = new HubConnectionBuilder() - .WithUrl("https://localhost:7167/ChatHub") + .WithUrl("https://localhost:7167/chatHub") //.WithUrl("http://localhost:61877") .WithAutomaticReconnect() .Build(); diff --git a/UI/ChatSignalR/src/global.json b/UI/ChatSignalR/src/global.json index be9c4cb99..a599a25e8 100644 --- a/UI/ChatSignalR/src/global.json +++ b/UI/ChatSignalR/src/global.json @@ -4,6 +4,8 @@ "Uno.Sdk": "5.3.96" }, "sdk":{ - "allowPrerelease": false + "version": "8.0.100", + "allowPrerelease": false, + "rollForward": "latestFeature" } }