diff --git a/real-time-app/real-time-app/Dockerfile b/real-time-app/SignalRApp/Dockerfile similarity index 100% rename from real-time-app/real-time-app/Dockerfile rename to real-time-app/SignalRApp/Dockerfile diff --git a/real-time-app/SignalRApp/Hubs/MessageHub.cs b/real-time-app/SignalRApp/Hubs/MessageHub.cs new file mode 100644 index 0000000..ae31879 --- /dev/null +++ b/real-time-app/SignalRApp/Hubs/MessageHub.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.SignalR; + +namespace SignalRApp.Hubs; +public class UserMessage +{ + public required string Sender { get; set; } + public required string Content { get; set; } + public DateTime SentTime { get; set; } +} +public class MessagingHub : Hub +{ + private static readonly List MessageHistory = new List(); + public async Task PostMessage(string content) + { + var senderId = Context.ConnectionId; + var userMessage = new UserMessage + { + Sender = senderId, + Content = content, + SentTime = DateTime.UtcNow + }; + MessageHistory.Add(userMessage); + await Clients.Others.SendAsync("ReceiveMessage", senderId, content, userMessage.SentTime); + } + public async Task RetrieveMessageHistory() => + await Clients.Caller.SendAsync("MessageHistory", MessageHistory); +} \ No newline at end of file diff --git a/real-time-app/SignalRApp/Program.cs b/real-time-app/SignalRApp/Program.cs new file mode 100644 index 0000000..23f2638 --- /dev/null +++ b/real-time-app/SignalRApp/Program.cs @@ -0,0 +1,25 @@ +using SignalRApp.Hubs; +var builder = WebApplication.CreateBuilder(args); +// Configure CORS +builder.Services.AddCors(options => +{ + options.AddPolicy("CorsPolicy", policy => + { + policy.WithOrigins("http://localhost:3000") + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); + }); +}); +// Add SignalR services +builder.Services.AddSignalR(); +var app = builder.Build(); +// Use CORS with the specified policy +app.UseCors("CorsPolicy"); +// Use default files and static files +app.UseDefaultFiles(); +app.UseStaticFiles(); +// Map the MessagingHub to the "/hub" endpoint +app.MapHub("/hub"); +// Run the application +app.Run(); \ No newline at end of file diff --git a/real-time-app/real-time-app/Properties/launchSettings.json b/real-time-app/SignalRApp/Properties/launchSettings.json similarity index 100% rename from real-time-app/real-time-app/Properties/launchSettings.json rename to real-time-app/SignalRApp/Properties/launchSettings.json diff --git a/real-time-app/real-time-app/real-time-app.csproj b/real-time-app/SignalRApp/SignalRApp.csproj similarity index 92% rename from real-time-app/real-time-app/real-time-app.csproj rename to real-time-app/SignalRApp/SignalRApp.csproj index ba1e387..fa8dce5 100644 --- a/real-time-app/real-time-app/real-time-app.csproj +++ b/real-time-app/SignalRApp/SignalRApp.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - real_time_app + SignalRApp Linux diff --git a/real-time-app/real-time-app/appsettings.Development.json b/real-time-app/SignalRApp/appsettings.Development.json similarity index 100% rename from real-time-app/real-time-app/appsettings.Development.json rename to real-time-app/SignalRApp/appsettings.Development.json diff --git a/real-time-app/real-time-app/appsettings.json b/real-time-app/SignalRApp/appsettings.json similarity index 100% rename from real-time-app/real-time-app/appsettings.json rename to real-time-app/SignalRApp/appsettings.json diff --git a/real-time-app/real-time-app/real-time-app.http b/real-time-app/SignalRApp/real-time-app.http similarity index 100% rename from real-time-app/real-time-app/real-time-app.http rename to real-time-app/SignalRApp/real-time-app.http diff --git a/real-time-app/real-time-app.sln b/real-time-app/real-time-app.sln index e12eb67..da77dfa 100644 --- a/real-time-app/real-time-app.sln +++ b/real-time-app/real-time-app.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "real-time-app", "real-time-app\real-time-app.csproj", "{1E00B034-8D96-4012-9DA0-A7CB3FAA6DA5}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignalRApp", "SignalRApp\SignalRApp.csproj", "{1E00B034-8D96-4012-9DA0-A7CB3FAA6DA5}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A7453357-B0EC-464E-8CFA-3D2E34977FF6}" ProjectSection(SolutionItems) = preProject diff --git a/real-time-app/real-time-app/Program.cs b/real-time-app/real-time-app/Program.cs deleted file mode 100644 index 161f695..0000000 --- a/real-time-app/real-time-app/Program.cs +++ /dev/null @@ -1,44 +0,0 @@ -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseHttpsRedirection(); - -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => - { - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; - }) - .WithName("GetWeatherForecast") - .WithOpenApi(); - -app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} \ No newline at end of file