Skip to content

Commit

Permalink
Start with signalR
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Feb 14, 2024
1 parent c555122 commit 6a56085
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions MadWorldVPS/MadWorld.ShipSimulator.API/Hubs/CompanyHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.SignalR;

namespace MadWorld.ShipSimulator.API.Hubs;

public class CompanyHub : Hub
{

}
19 changes: 19 additions & 0 deletions MadWorldVPS/MadWorld.ShipSimulator.API/Hubs/CompanyLiveUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MadWorld.ShipSimulator.Domain.Companies;
using Microsoft.AspNetCore.SignalR;

namespace MadWorld.ShipSimulator.API.Hubs;

public class CompanyLiveUpdater : ICompanyLiveUpdater
{
private readonly IHubContext<CompanyHub> _hubContext;

public CompanyLiveUpdater(IHubContext<CompanyHub> hubContext)
{
_hubContext = hubContext;
}

public async Task UpdateCompanyAsync(Company company)
{
await _hubContext.Clients.User(company.UserId.ToString()).SendAsync("CompanyUpdated", company);
}
}
12 changes: 12 additions & 0 deletions MadWorldVPS/MadWorld.ShipSimulator.API/Hubs/HubExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using MadWorld.ShipSimulator.Domain.Companies;

namespace MadWorld.ShipSimulator.API.Hubs;

public static class HubExtensions
{
public static void AddHubs(this WebApplicationBuilder builder)
{
builder.Services.AddSignalR();
builder.Services.AddScoped<ICompanyLiveUpdater, CompanyLiveUpdater>();
}
}
5 changes: 4 additions & 1 deletion MadWorldVPS/MadWorld.ShipSimulator.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MadWorld.Shared.Infrastructure.Databases;
using MadWorld.Shared.Infrastructure.Settings;
using MadWorld.ShipSimulator.API.Endpoints;
using MadWorld.ShipSimulator.API.Hubs;
using MadWorld.ShipSimulator.Application.CommonLogic.Extensions;
using MadWorld.ShipSimulator.Infrastructure.Database;
using MadWorld.ShipSimulator.Infrastructure.Database.Extensions;
Expand Down Expand Up @@ -55,6 +56,7 @@ private static void Main(string[] args)

builder.AddApplication();
builder.AddDatabase();
builder.AddHubs();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
Expand All @@ -81,7 +83,7 @@ private static void Main(string[] args)
});

builder.Services.AddAuthorization();

builder.Services.AddHealthChecks();

builder.Services.Configure<ForwardedHeadersOptions>(options =>
Expand Down Expand Up @@ -144,6 +146,7 @@ private static void Main(string[] args)

apiBuilder.AddCompanyEndpoints();
apiBuilder.AddDangerEndpoints();
apiBuilder.MapHub<CompanyHub>("/CompanyHub");

app.MigrateDatabase<ShipSimulatorContext>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MadWorld.ShipSimulator.Domain.Companies;

public interface ICompanyLiveUpdater
{
Task UpdateCompanyAsync(Company company);
}

0 comments on commit 6a56085

Please sign in to comment.