Skip to content

Commit

Permalink
Add SignalR
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Sep 29, 2024
1 parent 964fa56 commit 0a1a715
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Clients.Admin.Web/Admin.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" />
<PackageReference Include="Microsoft.Extensions.Http" />
</ItemGroup>

Expand All @@ -22,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="..\Clients.DefaultStyle\DefaultStyle.csproj" />
<ProjectReference Include="..\ClientSdk.Admin.Bff\ClientSdk.Admin.Bff.csproj" />
<ProjectReference Include="..\ClientSdk.Api.MessageBus\ClientSdk.Api.MessageBus.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 16 additions & 4 deletions src/Clients.Admin.Web/Pages/MessageBuses/MessageBus.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@using MadWorldNL.MantaRayPlan.Events
@using MadWorldNL.MantaRayPlan.Web.Services.Events
@using MadWorldNL.MantaRayPlan.Web.Services.MessageBuses
@page "/MessageBus"

@inject IMessageBusService Service;
@inject IMessageBusService MessageBusService;
@inject EventsService EventsService

<h1>Message Bus Test Page</h1>
@if (_isLoaded)
Expand All @@ -16,19 +19,28 @@

@code {
private bool _isLoaded;
private int _counter = 0;
private int _counter;

protected override async Task OnInitializedAsync()
{
var status = await Service.GetStatusAsync();
var status = await MessageBusService.GetStatusAsync();
_counter = status.counter;
_isLoaded = true;

EventsService.EventReceived += NewEventReceived;

await EventsService.StartAsync();
await base.OnInitializedAsync();
}

private void NewEventReceived(MessageBusStatusEvent @event)
{
_counter = @event.Count;
StateHasChanged();
}

private async Task UpdateStatus()
{
await Service.PostNewStatusAsync();
await MessageBusService.PostNewStatusAsync();
}
}
2 changes: 2 additions & 0 deletions src/Clients.Admin.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MadWorldNL.MantaRayPlan.Web;
using MadWorldNL.MantaRayPlan.Web.Configurations;
using MadWorldNL.MantaRayPlan.Web.Services.Events;
using MadWorldNL.MantaRayPlan.Web.Services.MessageBuses;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
Expand All @@ -21,5 +22,6 @@
});

builder.Services.AddScoped<IMessageBusService, MessageBusService>();
builder.Services.AddSingleton<EventsService>();

await builder.Build().RunAsync();
45 changes: 45 additions & 0 deletions src/Clients.Admin.Web/Services/Events/EventsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using MadWorldNL.MantaRayPlan.Events;
using MadWorldNL.MantaRayPlan.Web.Configurations;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Options;

namespace MadWorldNL.MantaRayPlan.Web.Services.Events;

public class EventsService : IAsyncDisposable
{
private bool _isStarted;
private readonly HubConnection _hubConnection;

public event Action<MessageBusStatusEvent>? EventReceived;

public EventsService(IOptions<ApiSettings> settings)
{
_hubConnection = new HubConnectionBuilder()
.WithUrl($"{settings.Value.Address}Events")
.Build();

_hubConnection.On<MessageBusStatusEvent>("NewEvent", (@event) =>
{
EventReceived?.Invoke(@event);
});
}

public Task StartAsync()
{
if (_isStarted)
{
return Task.CompletedTask;
}

_isStarted = true;
return _hubConnection.StartAsync();
}

public async ValueTask DisposeAsync()
{
await _hubConnection.StopAsync();
await _hubConnection.DisposeAsync();

GC.SuppressFinalize(this);
}
}
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="MassTransit.RabbitMQ" Version="8.2.5" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
Expand Down
7 changes: 6 additions & 1 deletion src/Server.Logic.Domain/MessageBuses/MessageBusStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ namespace MadWorldNL.MantaRayPlan.MessageBuses;

public class MessageBusStatus
{
public int Id { get; set; }
public int Id { get; init; }
public int Count { get; set; }

public void IncrementCount()
{
if (Count >= int.MaxValue)
{
Count = 0;
}

Count++;
}
}

0 comments on commit 0a1a715

Please sign in to comment.